How to check parent & child category

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' ); 
       ]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x