Support » How-To and Troubleshooting » is category child?

is category child?

  • How do I write a conditional statement that tests whether a category is a child of a category. My parent category is “Questions” with a bunch of children categories.

    Using is_category(‘questions’) only works at the parent level, but not at the child level.


Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    YOu need to use os_child function to check the child category or you can use an plugin to check the category is child or not..


    Shane G.

    Where, meaning, what template (single.php, category.php, index.php) are you testing this in?

    Or are you trying to do this test in a post loop?

    I need to use it in the header.php file to handle the active page highlight for my navigation bar.


    I figured I should just post my code so you can see what I mean if that will help.

    <ul class="nav-bar">
    			<li <?php if ( is_home() ) { echo ' class="current"'; } ?>><a href="<?php echo get_option('home'); ?>">HOME</a></li>
    			<li <?php if ( is_category('questions') ) { echo ' class="current"'; } ?>><a href="/rathers">QUESTIONS</a></li>
    			<li <?php if ( is_page('tags') ) { echo ' class="current"'; } ?>><a href="/tags">TAGS</a></li>
    			<li <?php if ( is_page('store') ) { echo ' class="current"'; } ?>><a href="/store">STORE</a></li>
    			<li <?php if ( is_page('submit') ) { echo ' class="current"'; } ?>><a href="/submit">SUBMIT</a></li>
    			<li <?php if ( is_page('about') ) { echo ' class="current"'; } ?>><a href="/about">ABOUT</a></li>
    			<li <?php if ( is_category('blog') ) { echo ' class="current"'; } ?>><a href="/blog">BLOG</a></li>

    It basically just changes the css class based on which page its on. So it works when I view the “Questions” category archive, but not when I view an actual post within the subcategories. I know I could just manually add all my subcategories as a conditional statement, but I have many so I’d like to find a cleaner way to handle this. Also, my blog is set up as its own category which also has the same problem.

    Thanks again!

    There is the cat_is_ancestor_of function but that could return true if a category was a grandchild of another category. Try this:

    // in category template, test if queried category is child of another category
    if ( is_category() ) {
    $parent = 8;
    $categories = get_categories('include='.get_query_var('cat'));
    if ( $categories[0]->category_parent == $parent ) {
    echo 'category ' . $categories[0]->name . ' is a child of category ' . $parent;
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘is category child?’ is closed to new replies.