How to check parent & child category

The question:

I just want to display a section in the parent category. And I want to display another section in the child category. How to do it?

      if ( is_category() ) {
          get_template_part( 'template-parts/banner/tab-1' , 'xcde' ); 
        } else {
           get_template_part( 'template-parts/banner/tab-2' , 'xcde' ); 

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

On your category template you can use get_queried_object() to get the current WP_Term object. From that you can check the parent property, which is 0, if it is a parent, or some integer, if it is a child term as it returns the parent term’s ID.

$current_term = get_queried_object();
if ( $current_term->parent ) { 
    // child term
} else {
    // parent term

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment