【问题标题】:Twig Wordpress Repeater within a repeater中继器中的 Twig Wordpress 中继器
【发布时间】:2019-04-17 13:30:57
【问题描述】:

我想就使用 Twig 在 Wordpress 上的转发器中的转发器寻求帮助。服务部分正确显示,但服务部分中的功能部分未显示。

这是 Wordpress ACF 的截图。Click Me

下面是我当前使用的代码。 请指教。谢谢!

{% extends "page.twig" %}
{% block additional %}

<div id="page-services">
  <section id="services">  
    <div class="row small-up-1 large-up-1">
    <div class="small-12 medium-11 large-9 columns small-centered">
    <div class="services-grid animated fadeIn wow">
    <p align="center">
      {{post.services_desc}}
    </p>
</div>
</div>
</div>
<div class="line centered"></div>
</div>

<center>
<div class="row">
<div class="small-12 medium-11 large-9 columns small-centered">
<div class="features-header animated fadeIn wow">

{% for item in post.get_field('services_ist') %}

  <div class="column services">
    <h2 class="capitalize bold">
      {{item.services_title}}
    </h2>

  {% if item.services_subtitle %}
    <h4 class="subtitle">
      {{item.services_subtitle}}
    </h4>

<div class="line thin"></div>

  {% endif %}

  {% if item.services_content %}
    <div class="description">
      {{item.services_content}}
      <br><br>
    </div>
  {% endif %}

{% if feats.services_feat %}
  {% for feats in post.get_field('services_feat') %}
    <p>{{feats.feat_title}}</p>
  {% endfor %}


  {% if feats.feats_desc %}
    <h4 class="feats description">
      {{feats.feats_desc}}
    </h4>
  {% endif %}

{% endif %}

  </div>
{% endfor %}
</center>
</div>
</div>
</div>
  </section>
</div>
{% endblock %}

【问题讨论】:

    标签: wordpress twig advanced-custom-fields timber nested-repeater


    【解决方案1】:

    正如ACF Integration Guide 所说,当您尝试访问嵌套转发器字段时,不应再次使用get_field()

    当您在外部 ACF 字段上运行 get_field 时,内部的所有内容都已准备好被遍历。您可以通过 item_outer.inner_repeater 引用嵌套字段

    所以不要使用:

    {% for feats in post.get_field('services_feat') %}
    

    你应该使用:

    {% if feats.services_feat %}
        {% for feats in feats.services_feat %}
            <p>{{ feats.feat_title }}</p>
        {% endfor %}
    
        {# … #}
    {% endif %}
    

    【讨论】:

      【解决方案2】:

      我以前从未做过 Twig,但快速搜索让我有所收获。 将内部中继器更改为:

        {% for feats in services_ist.get_field('services_feat') %}
          <p>{{feats.feat_title}}</p>
        {% endfor %}
      

      这样第二个转发器就知道它是第一个转发器的子代,而不是帖子的直接子代。

      【讨论】:

      • 刚刚试过了,还是不行。谢谢你的尝试。
      猜你喜欢
      • 2019-02-03
      • 1970-01-01
      • 2014-04-02
      • 2020-12-19
      • 1970-01-01
      • 2019-03-30
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多