【问题标题】:ACF field in child/sub category子/子类别中的 ACF 字段
【发布时间】:2021-11-29 15:53:55
【问题描述】:

我在 woocommerce 子类别字段中有一个 ACF 字段。该字段在 wordpress 管理员中显示得很好,但是当我尝试显示子类别的自定义字段时,父自定义字段会显示出来。我如何显示 wordpress 的 ACF 子类别。这是我使用的显示父 ACF 字段的代码:

$current_category = single_cat_title("", false);

$term_id = get_queried_object_id($current_category);

echo get_field('designer_name', 'product_cat_' . $term_id);

【问题讨论】:

    标签: wordpress advanced-custom-fields acfpro


    【解决方案1】:

    根据 ACF 文档,它应该是这样的,而不是你的:

    $term = get_queried_object();    
    $term_id = $term->term_id; 
    
    echo get_field('designer_name', 'term_' . $term_id);
    

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

    编辑 上面报了获取父字段值,来显示子项:

    $term = get_queried_object();    
    $term_id = $term->term_id;
    $taxonomy =  $term->taxonomy;
    
    $children_terms = get_term_children(  $term_id, $taxonomy );
    
    if( !is_wp_error( $children_terms ) && $children_terms ){
        foreach( $children_terms as $child ){
            echo get_field('designer_name', 'term_' . $child );
       }
    }
    

    https://developer.wordpress.org/reference/functions/get_term_children/

    【讨论】:

    • 这显示了 PARENT acf 字段,我需要它来显示子 acf 字段
    • 好的,以为您已经在子分类页面上。将更新我的答案。
    • 嗨,tammi,谢谢,这几乎可以工作,但是它显示了所有子类别的自定义字段值,而不是仅与该类别相关的字段值
    • @PhilipSavarirayan 我应该能够帮助您显示正确的,但如果无法访问该站点和代码,则很难猜测。有什么方法可以告诉我如何在代码中知道哪个 term_id 或 term slug 或 name 是相关的当前类别?
    • 如果你回显 get_query_var($taxonomy); 你会得到什么@PhilipSavarirayan?
    猜你喜欢
    • 1970-01-01
    • 2019-03-30
    • 2016-02-04
    • 1970-01-01
    • 2020-10-25
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多