Support

Account

Home Forums ACF PRO Get_Field Not Showing in Category Array

Solved

Get_Field Not Showing in Category Array

  • Hello, this seems like it should be really straightforward, but I can’t my field to populate. I am making a category navigation on my homepage, pulling in category names and I wanted to assign a font awesome icon through ACF. Easy right? I don’t know why its not working. Here is my code:

    <?php
    
    	$taxonomy     = 'product_cat';
    	$orderby      = 'name';
    	$show_count   = 0;      // 1 for yes, 0 for no
    	$pad_counts   = 0;      // 1 for yes, 0 for no
    	$hierarchical = 1;      // 1 for yes, 0 for no
    	$title        = '';
    	$empty        = 0;
    	$category_icon  = get_field('category_icon');
    
    	$args = array(
    				 'taxonomy'     => $taxonomy,
    				 'orderby'      => $orderby,
    				 'show_count'   => $show_count,
    				 'pad_counts'   => $pad_counts,
    				 'hierarchical' => $hierarchical,
    				 'title_li'     => $title,
    				 'hide_empty'   => $empty,
    	);
     $all_categories = get_categories( $args );
     foreach ($all_categories as $cat) {
    		if($cat->category_parent == 0) {
    				$category_id = $cat->term_id;
    
    				echo '<div class="cat-box"><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .
    			  $category_icon;
    				echo '</a></div>';
    
    				$args2 = array(
    								'taxonomy'     => $taxonomy,
    								'child_of'     => 0,
    								'parent'       => $category_id,
    								'orderby'      => $orderby,
    								'show_count'   => $show_count,
    								'pad_counts'   => $pad_counts,
    								'hierarchical' => $hierarchical,
    								'title_li'     => $title,
    								'hide_empty'   => $empty
    				);
    				$sub_cats = get_categories( $args2 );
    				if($sub_cats) {
    						foreach($sub_cats as $sub_category) {
    								echo  $sub_category->name ;
    						}
    				}
    		}
    }
    ?>

    Thank you!

  • Okay I figured it out. I trimmed off the subcategories, but my problem was that I was not associating category with the field and value. So the $cat was acting as the post id in the same way that a page will render an ACF value.

    <?php
    
    	$taxonomy     = 'product_cat';
    	$orderby      = 'name';
    	$show_count   = 0;      // 1 for yes, 0 for no
    	$pad_counts   = 0;      // 1 for yes, 0 for no
    	$hierarchical = 1;      // 1 for yes, 0 for no
    	$title        = '';
    	$empty        = 0;
    	$post_id 			= 1;
    	$args = array(
    				 'taxonomy'     => $taxonomy,
    				 'orderby'      => $orderby,
    				 'show_count'   => $show_count,
    				 'pad_counts'   => $pad_counts,
    				 'hierarchical' => $hierarchical,
    				 'title_li'     => $title,
    				 'hide_empty'   => $empty,
    	);
     $all_categories = get_categories( $args );
     foreach ($all_categories as $cat) {
    		if($cat->category_parent == 0) {
    				$category_id = $cat->term_id;
    				$icon = get_field('category_icon', $cat);
    
    				echo '<div class="cat-box"><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name;
    			  echo '<div class="clear"></div>'. $icon;
    				echo '</a></div>';
    		}
    }
    ?>
    
    • greati

    • March 24, 2020 at 11:42 pm

    hello, @toolboxwebdesign an you help me with this issue i am having ?
    https://support.advancedcustomfields.com/forums/topic/menu-children-category-field-in-other-language/

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.