Support

Account

Home Forums General Issues Output from related Taxonomy

Solving

Output from related Taxonomy

  • Hello, i have a CPT called codes and 2 taxonomies called shops and banners.

    in ACF i related the taxonomy banners to taxonomy shops with the taxonomy add on. Now i want to output the custom fields from banners on the page where i output my taxonomy shops.

    normally if i want to output a field from a taxonomy i use:

    <?php the_field('field name from taxonomy shops ','shops_'.$wp_query->queried_object->term_id);?>

    that’s really working great.

    but now i want to output a field from the related taxonomy: banners.

    i tried

    <?php the_field('field name from taxonomy banners','shops_'.$wp_query->queried_object->term_object);?>

    but it only give me a #id and not the data from the custom field of taxonomy banners.

    What’s going wrong?

  • Hi @ron Willemse

    The above cannot be done in 1 single function. You will need to break it into 2 steps.

    1. use get_field to find the related taxonomy.
    2. Depending on the return type setting (ID / Object), you need to then use that data to load the field from.

    Does that help?

    Thanks
    E

  • Thnx, first of all i like to thank you for the ACF plugin. the second thing is i’m a frontend developer and not really good in php. Can you give me a example?

  • Hi @ron Willemse

    I’m not 100% sure, but I think your code should be something like this:

    <?php 
    
    // vars
    $current_term = $wp_query->queried_object;
    
    // use get_field to find the related taxonomy.
    $related_term = get_field('field name from taxonomy shops ', $current_term->taxonomy . '_' . $current_term->term_id);
    
    // Depending on the return type setting (ID / Object), you need to then use that data to load the field from.
    $related_term_field = get_field('field_name', $related_term->taxonomy . '_' . $related_term->term_id );
    
    echo $related_term_field;
    
    ?>

    If that deosn’t look write, perhaps you could clearly define the steps needed to get the data you want from the current taxonomy term?

    Thanks
    E

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

The topic ‘Output from related Taxonomy’ is closed to new replies.