get_field()在我的foreach循环中不起作用。

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

我为我的产品类别做了一个自定义字段,这样我就可以使用ACF插件选择相应的图标。我把所有的类别都这样调用(但是自定义字段的值什么都没有返回,因为我试过var_dumping)。

<?php 

            $orderby = 'date';
            $order = 'DESC';
            $hide_empty = false ;
            $cat_args = array(
                'orderby'    => $orderby,
                'order'      => $order,
                'hide_empty' => $hide_empty,
            );

            $product_categories = get_terms( 'product_cat', $cat_args );

            if( !empty($product_categories) ){
                foreach ($product_categories as $key => $category) {
                    $icon = get_field('icon_classname');
                    if (empty($icon)) { $icon = 'flaticon-002-coat'; }
            ?>

            <div class="cat-item">
                <div class="cat cat-1">
                    <div class="cat-wrap">
                        <a class="category" href="<?php echo get_term_link($category); ?>">
                            <i class="icon fimanager <?php echo $icon; ?>"></i>
                            <span class="cat-title"><?php echo $category->name; ?></span>
                        </a>
                    </div>
                </div>
            </div>

            <?php

                } wp_reset_postdata();
            }

            ?>
wordpress woocommerce advanced-custom-fields
1个回答
0
投票

请检查一下:你应该替换这两行。

 foreach( $product_categories as $cat) {
  $icon = get_field('icon_classname', 'category_'.$cat->term_id);
© www.soinside.com 2019 - 2024. All rights reserved.