Kryslerr Lisondra
Kryslerr Lisondra

Reputation: 5

get_field() does not work inside my foreach loop

I've made a custom field for my product categories so that I could select icons accordingly using ACF plugin. I called all the categories like this (But the values of the custom fields return nothing as I've tried var_dumping it):

<?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();
            }

            ?>

Upvotes: 0

Views: 569

Answers (1)

eli
eli

Reputation: 162

please check it :
you should replace these two lines :

 foreach( $product_categories as $cat) {
  $icon = get_field('icon_classname', 'category_'.$cat->term_id);

Upvotes: 0

Related Questions