1

I have a custom taxonomy: product_range and it has 3 levels:

(1 - Parent) Product

(2 - Child) Product Category

(3 - Grandchild) Product Sub Category

I need to display different content depending on which page the user is on, how do I check whether or not the user is on the parent page and then display something, if the user is on the child page display something else and again if they're on the grandchild page.

So far I have this that displays different content depending on whether the user is on the parent or child page but I need to go one level deeper:

<?php 
     $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
     if($term->parent > 0) { 
       // THIS IS THE CHILD PAGE
       include(locate_template('product-range-category-index.php'));
     } 
     else { 
       // THIS IS THE PARENT PAGE
       include(locate_template('associated-product-ranges.php'));
     }
?>
  • What is the url structure of child and grandchild categories? – ClemC Aug 14 '17 at 11:26
  • @ClemC the URL structure is: http://xxxxx/product-range/trackballs/mechanical-trackballs/16mm and the terms are all unique and the client will be adding more. – Neelam Khan Aug 14 '17 at 11:45
0

Try get_ancestors():

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$ancestors = get_ancestors( $term->term_id, 'product_range', 'taxonomy' );

$hierarchy_levels = count( $ancestors );

switch ( $hierarchy_levels ) {
    case 0:
        // THIS IS THE PARENT CATEGORY
        // DO YOUR STUFF
        break;

    case 1:
        // THIS IS THE PARENT CATEGORY
        // DO YOUR STUFF
        break;

    case 2:
        // THIS IS THE PARENT CATEGORY
        // DO YOUR STUFF
        break;
}

Another solution (but constraining) would be determining which hierarchy the user is currently on based on the url structure. The constraint would be that you have to keep a "simple" or "classical" hierarchy structure in your category URLs, as follow:

  • https://example.com/parent/child/grandchild/
  • https://example.com/parent/child/
  • https://example.com/parent/
  • Thats great it works, out of curiosity what is the other solution you mentioned? My URL structure is the way you describe above. If there is a better way to achieve the same result that would be great. – Neelam Khan Aug 14 '17 at 13:17
  • @NeelamKhan, I'm not really sure that would be a "better way" at all honestly... The only pros I can see concerning this second approach is that it would avoid using get_term_by() and so avoid a DB query - for the cost of a little less "integrated" and so "waterproof" solution... More concretely, what I'm thinking to is to get the URL, then from it, getting the number of nested categories via a preg_match() or by counting the number of slash for example... This would give you the same $hierarchy_levels value obtained in the code above... – ClemC Aug 14 '17 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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