問題:我需要從靈活的內容欄位中獲取當前布局索引。ACF 已經為此提供了內置功能 - https://www.advancedcustomfields.com/resources/get_row_index/
木材模板:
{% extends "base.twig" %}
{% block content %}
{% for flex_content in post.meta('flex_components') %}
{{ get_row_index() }} <!-- I'm stuck here -->
{% include [flex_content.acf_fc_layout|sanitize ~ '.twig'] ignore missing %}
{% endfor %}
{% endblock %}
我目前的解決方案:正在使用一個簡單的計數器,但我想學習如何使用 get_row_index() 函式(如果可能的話)來實作它。
{% extends "base.twig" %}
{% block content %}
{% set my_counter = 1 %}
{% for flex_content in post.meta('flex_components') %}
{% include [flex_content.acf_fc_layout|sanitize ~ '.twig'] ignore missing %}
{% set my_counter = my_counter 1 %}
{% endfor %}
{% endblock %}
uj5u.com熱心網友回復:
不幸的是,get_row_index()在使用 Timber 時不能使用,因為該函式依賴于 ACF 在使用時設定的全域變數have_rows(),而我們在 Timber 中沒有。這與 Timber 試圖擺脫的 WordPress Loop 類似。
在嫩枝,你可以隨時使用
回圈變數一個內for回圈。此變數為您提供所需的計數器。
{% extends "base.twig" %}
{% block content %}
{{ dump(loop.index) }}
{% for flex_content in post.meta('flex_components') %}
{% include [flex_content.acf_fc_layout|sanitize ~ '.twig'] ignore missing %}
{% endfor %}
{% endblock %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/405235.html
標籤:
