Philip Savarirayan
Philip Savarirayan

Reputation: 37

ACF field in child/sub category

I have a ACF field in woocommerce sub category field. the field shows up just fine in the wordpress admin but when i try to display the custom field of the child category the parent custom field shows up. how do i show ACF child categories of wordpress. this is the code im using that displays the parents ACF field:

$current_category = single_cat_title("", false);

$term_id = get_queried_object_id($current_category);

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

Upvotes: 0

Views: 2313

Answers (1)

Tami
Tami

Reputation: 744

As per ACF documentation, it should be like this instead of yours:

$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/

EDIT The above was reported to get the parent field value, to display the children:

$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/

Upvotes: 1

Related Questions