ACF get_field 在带有 foreach 循环的函数内不返回值

问题描述 投票:0回答:1

我有一个函数可以循环当前类别的所有 Woocommerce 子类别。在子类别循环内部有一个高级自定义字段,应该显示一个图标。但我做错了什么,所以它没有出现。我做错了什么?

这就是功能。

function pab_display_subcategories() {
    if ( is_product_category() ) {

        $term_id  = get_queried_object_id();
        $taxonomy = 'product_cat';

        // Get subcategories of the current category
        $terms    = get_terms([
            'taxonomy'    => $taxonomy,
            'hide_empty'  => false,
            'parent'      => get_queried_object_id()
        ]);

        $output = '
        <div class="bde-wooproductslist breakdance-woocommerce">
            <div class="woocommerce">
                <ul class="products">';
        
                    // Loop through product subcategories WP_Term Objects
                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, $taxonomy );
                        
                        $output .= '<li class="product-category product pab-cat-list"><a href="'. $term_link .'">';
                        
                        $cat_icon = get_field( 'cat_icon', $term->term_id );
                        if ( $cat_icon ) :
                            $output .= '<div class=""><img src="'. esc_url( $cat_icon['url'] ). '" alt="'. esc_attr( $cat_icon['alt'] ) .'" class="pab-wc-category-icon"></div>';
                        endif;

                        $output .= '<div class="pab-cat-list-text">'. $term->name .' '. $term->term_id .'<i class="fa-solid fa-chevron-right"></i></div>
                        </a></li>';
                    }

                echo $output . '
                </ul>
            </div>
        </div>';
    }
}
add_shortcode( 'pab_display_subcategories_content','pab_display_subcategories' );
php woocommerce advanced-custom-fields shortcode code-snippets
1个回答
0
投票

试试这个,希望能成功。

get_field( 'cat_icon', 'term_' . $term->term_id );
© www.soinside.com 2019 - 2024. All rights reserved.