【问题标题】:Using an ACF gallery with Timber/Twig使用带有 Timber/Twig 的 ACF 库
【发布时间】:2018-12-03 00:10:49
【问题描述】:

我正在使用 Timber(Twig 的 WordPress 插件实现)和 Advanced Custom Fields 插件库字段。我在自定义帖子类型下有一个工作画廊,因此 ACF 和 Timber 在网站的其他地方工作,但不适用于标准页面。当我尝试向页面添加图库时,我最终得到的只是 img src 标记的空标记。

我在模板文件夹的 page.php 中有这个:

$context = Timber::get_context();
$page = new TimberPost();

$context['page'] = $page;

if ($photos = $page->get_field('photos')) {
    $context['photos'] = $photos;
}

我在主题的 templates/page/ 文件夹中的 default.twig 中有这个(为简单起见,删除了 html):

{% if page.photos %}

{% for p in page.photos %}

<img src="{{ p.sizes.gallery|relative }}" alt="{{ p.alt }}" /> 

{{ p.caption }}

{% endfor %} 

{% endif %}

这导致页面源&lt;img src="" alt=""&gt;

如果我在for p in page.photos 语句中使用{{ dump(photos) }},它会转储我在后端图库字段中输入的图像数组。所以图像数组存在并且正在输出。 relative 扩展适用于所有帖子类型;删除它在这里没有区别。

那么为什么p.sizes.gallery|relative函数没有输出每张图片的url和标题呢?

【问题讨论】:

  • 您将数据附加到$context['photos'],所以您的代码不应该检查if photos 并迭代为`for p in photos` 吗?
  • @Owi,啊,就是这样。谢谢。我只是没看到。将其添加为答案。
  • 我刚刚添加了答案,谢谢 ;)

标签: twig advanced-custom-fields timber


【解决方案1】:

您将数据附加到 $context['photos'] 所以我相信您应该更改代码以检查 if photos 并迭代为 for p in photos

【讨论】:

    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多