通过 ACF 将图像馈送到 Twig / Timber WordPress

问题描述

在我的树枝模板中,我有以下内容

{% include '@hc/components/home-hero.twig' with {
    'image': '/assets/images/home-hero.png','title' : 'Title goes here',

要从 wordpress 上的 ACF 字段输入数据,我执行以下操作:

{% include '@hc/components/home-hero.twig' with {
    'image': Image(data.hero_image),'title' : data.title_field,

'title' 工作正常,任何其他类型的字段也是如此。由于某种原因,我无法让图像通过。我尝试了很多变体,例如:

{% set header_image %}  {{ post.Meta('header_image') }} {% endset %} 
{% include '@hc/components/home-hero.twig' with {
    'image': header_image,

在转储 header_image 时,我得到以下信息:

object(Twig\Markup)#2380 (2) {["content":protected]=>string(66) "url of image is in here" ["charset":protected]=>string(5) "UTF-8" }

有人知道这是怎么回事吗?气死我了!

谢谢

解决方法

根据 timber / acf cheatsheet 您可能应该使用以下内容:

{% include '@hc/components/home-hero.twig' with {
        'image': Image(post.meta('header_image)),'title' : data.title_field
    }
%}

否则,如果您可以添加 home-hero.twig 的代码,以查看如何使用/查询图像变量,请参阅 similiar issue