首页 > 解决方案 > Jekyll - 无法获取 url 和作者姓名

问题描述

我正在尝试使用以下代码获取作者详细信息以显示在他们的博客附近

{% assign author = site.authors | where: 'name', post.author %}
{% if author %}
- <a href="{{ author.url }}">{{ author.name }}</a>
{% endif %}

当我尝试使用 {{ author }} 打印作者时,它会在视图中显示作者页面,但是当我尝试打印 {{author.url}} 或 {{author.name}} 之类的任何内容时,它不会打印任何事物。

此外,当我使用以下代码单独打印作者详细信息时,它会显示详细信息

{%- for aut in site.authors-%}
{{aut.name}}
{{aut.url}}
{%- endfor -%}

任何帮助,将不胜感激。

标签: jekyllliquid

解决方案


{% assign author = site.authors | where: 'name', post.author %}

这将返回一个包含匹配元素的数组,或者nil如果没有找到匹配元素。

如果匹配元素应该存在并且是唯一的,则可以使用第一个数组过滤器来获取它,如下所示:

{% assign author = site.authors | where: 'name', post.author | first %}

推荐阅读