使用 WordPress 链接到 Timber/Twig 中的图像

问题描述

我是 Twig 和 Timber 世界的新手,我刚刚开始了一份新工作,我很难掌握使用它。 我已经在 Twig 中获得了一个完整的设计,我已将其转换为 wordpress 站点,现在我正在尝试通过 ACF 使其可编辑。在我的 front-page.PHP 中,我设置了以下内容

$context['data'] = get_fields();

然后在我提供的设计的主页(树枝文件)中,这是一段代码

{% include [
    'override/pullquote.twig','components/pullquote.twig'
    ] with {
    'title': data.my_story_title,'section_class': 'light pullquote-standalone text-center','image': data.my_story_image,'blockquote': data.my_story_quote,'cite': '','link_footer': "Read Ramon's story",'link_path': '#',} %}

所以我为 titleblockquote 创建了 ACF 选项,并使用了 data.my_story_titledata.my_story_quote > 这些工作正常。但是,data.my_story_image 没有,它什么也不做。我哪里出错了?大概我必须用它和形象来扩展它,但我不确定如何。这可能是一件简单的事情 - 任何帮助将不胜感激。

谢谢:)

解决方法

ACF 通常会返回图像 ID,因此您可能需要将字段值转换为 TimberImage,然后再将其传递给模板。试试:

'image': Image( data.my_story_image )

还有一些关于将 ACF 与 Timber/twig 结合使用的有用文档:https://timber.github.io/docs/guides/acf-cookbook/