【问题标题】:ACF: How to get sub field in category.php page?ACF:如何在 category.php 页面中获取子字段?
【发布时间】:2019-02-06 07:56:07
【问题描述】:

我使用 ACF 为术语创建了字段。我还使用以下代码创建了category.php:

<?php

// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category' );

// we will use the first term to load ACF data from
if ( !empty( $terms ) ) {

    $term = array_pop($terms);

    if ( have_rows( 'page_banner' ) ) :

        // loop through the rows of data

        while ( have_rows( 'page_banner' ) ) : the_row();

            $custom_field = get_sub_field( 'small_title', $term );

            echo $custom_field;

        endwhile;

    else :

    endif;

}

?>

但我没有得到结果,需要你的帮助。谢谢。

【问题讨论】:

  • get_sub_field() 的第二个参数不是术语。就是格式值,get_sub_field($sub_field_name, $format_value);
  • 对不起,我听不懂。你能提供更多细节吗
  • 当您调用 get_sub_field() 时,您正在传递 $term。但根据文档,第二个参数应该是 format_value。你可以在这里查看。 advancedcustomfields.com/resources/get_sub_field

标签: wordpress advanced-custom-fields acfpro


【解决方案1】:

我刚刚检查了以下代码。使用以下代码。

<?php

// get the current taxonomy term
$term = get_queried_object();

if ( have_rows( 'page_banner', $term ) ) {
    while( have_rows( 'page_banner', $term ) ) {
        the_row();
        the_sub_field( 'small_title' );
        echo '<br />';
    }
}

参考

https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/ https://www.advancedcustomfields.com/resources/have_rows/

【讨论】:

  • @VenkateshSrikanth 快乐是我的全部。 :)
猜你喜欢
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多