0

I have custom post type "product" and taxonomy is "product_cat". There are two child terms -Music and videos. Under Music i have two more child terms albums and single. Now when i go to music category i want to get all the child terms of the music. I am using the following code

<?php $page_id = get_the_id(); ?>

<?php $cat_terms = wp_get_object_terms( $page_id, 'product_cat' ); ?>

<pre><?php var_dump($cat_terms); ?></pre>


<?php foreach ($cat_terms as $terms  ) {?>
<?php $array = (array) $terms; ?>
<section id="engagement-rings">
  <div class="container">
    <div class="row">
      <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
        <div class="display-rings">
          <div class="heading-img text-center"><?php echo $array["name"]; ?></div>
          <div class="engagement rings-list clearfix">
            <ul class="list-unstyled clearfix">
            <?php
                   $args = array( 'post_type'=>'product',
                  'tax_query' => array(

                    array(
                      'taxonomy' => 'product_cat',
                      'field' => 'slug',
                      'terms' => $array["name"],
                    )
                  )
                );
                $postslist = get_posts( $args );


              ?>
                <?php foreach ($postslist as $post) : ?>
                    <?php $array1 = (array) $post; ?>

                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'shop_thumbnail' ); ?>
                    <li><a href=""><img src="<?php echo $image[0]; ?>" alt=""></a></li>
                <?php endforeach; ?>
            </ul>
            <a href="#" class="view-all">View all</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
<?php } ?>

But this is giving me the Music category and one child i.e albums . But i just want album and single .

Please help me out

1 Answer 1

1

I think you should rather use get_queried_object() to return the current page's term. From there you should get the taxonomy name, term ID and the parent.

Now here is my logic on this. Whenever you are on a parent term, ie. music or videos, the parent will return 0. You can use this to execute the code only if you are on a parent term page.

Here is the output from var_dump($cat_terms); on a child page

array(0) {
}

and here is from a parent page

array(2) {
  [0]=>
  int(119)
  [1]=>
  int(120)
}

119 and 120 is the child terms of my parent term

You have the taxonomy name and term ID, so you can use get_term_children to get the child terms from your terms music or videos. Like I said, you would only want to do this on the parent page. But that is all up to you.

Here is the code

<?php 
    $queried_object = get_queried_object('term');
        $tax = $queried_object->taxonomy;
        $term = $queried_object->term_id;
        $parent = $queried_object->parent;

    if( 0 == $parent ) {
        $cat_terms = get_term_children( $term, $tax );
         ?>
           <pre><?php var_dump($cat_terms); ?></pre>
         <?php 
        //<---DO SOMETHING ON PARENT TERM PAGE--->
    }else{
        //<---DO SOMETHING ON CHILD TERM PAGE
    }
?>
1
  • My pleasure. Glad I could help. Enjoy :-) Jul 31, 2014 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.