【问题标题】:Feed image through ACF to Twig / Timber WordPress通过 ACF 将图像馈送到 Twig / Timber WordPress
【发布时间】:2021-06-17 10:54:52
【问题描述】:

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

{% 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,

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

{% 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" }

有人知道发生了什么吗?快把我逼疯了!

谢谢

【问题讨论】:

  • data.hero_image 返回什么?如果是url或者id,试试Image(data.hero_image).src

标签: php wordpress twig timber


【解决方案1】:

根据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

【讨论】:

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