最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

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

Share Improve this question asked Jul 15, 2020 at 22:21 moorewebxmoorewebx 571 gold badge2 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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/reference/functions/get_ancestors/

发布评论

评论列表(0)

  1. 暂无评论