TransWikia.com

How to get the top level parent term Advanced Custom Field values in the child terms

WordPress Development Asked by moorewebx on October 30, 2021

I am using Advanced custom fields in my product_cat taxonomy to display fields on my product category pages. I would like to be able to edit the top parent product category and have those changes reflected on both the parent and child pages.

Here is the current code I am working with:

add_filter('ocean_after_page_header', 'woo_product_cat_header');
function woo_product_cat_header( $src ) {
    // ///////////////  CHEMICALS
     if (is_product_category()) {  
        $term = get_queried_object();
        $img_url = get_field('category_image_placeholder', $term);
        $description = get_field('category_description', $term); ?>

    <div class="woo-cat-page-header" style="background-image: url('<?php echo $img_url?>');"></div>

    <div class="container">
        <div id="woo-cat-description">
            <?php echo $description;?>
        </div>
    </div>
    
<?php } elseif ( has_term( 'other-category-here', 'product_cat' ) ) {
echo 'Something else';
}
  return $src;
}

As I understand it I need to change the $term = get_queried_object(); to something like:

function get_term_top_most_parent( $term_id, $taxonomy ) {
    $parent  = get_term_by( 'id', $term_id, $taxonomy );

I cannot figure this one out. Thanks

One Answer

Could look into using get_ancestors();. It will get an array of all of the ancestors of the item in order from lowest to highest.

See: https://developer.wordpress.org/reference/functions/get_ancestors/

Answered by Stephen Sabatini on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP