【问题标题】:Concatenate in an element acf fields or an objects在元素 acf 字段或对象中连接
【发布时间】:2021-06-22 19:21:10
【问题描述】:

我正在尝试连接 Twig 变量和 ACF 选项字段,但它不起作用。

我有两个页脚,我想根据正确的页面选择正确的页脚。

{% if post.slug == "page1" %}
    {% set pageType = "pages" %}
{% elseif post.slug == "page2" %}
    {% set pageType = "otherspages" %}
{% endif %}

<footer id="footer">
   <h2>{{ options.footer_~pageType~_titre }}</h2>
   <div>{{ options.footer_~pageType~_container }}<div>
</div>

根据我要显示的页脚,ACF 字段称为 footer_page_titre 或 footer_otherpage_titre

谢谢

【问题讨论】:

标签: object twig concatenation advanced-custom-fields timber


【解决方案1】:

尝试首先构造字段名称,例如使用 twig 格式过滤器,它通过替换占位符来格式化给定的字符串,类似于 sprintf,然后在选项数据数组中访问字段值。

构造字段名:

{% set footer_title = "footer_%s_title"|format(pageType) %}

通过数组键访问值

<h2>{{ options[footer_title] }}</h2>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2014-01-29
    • 2012-02-14
    • 1970-01-01
    相关资源
    最近更新 更多