Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We have is_category() for categories, however I m not getting a conditional tags for subcategories, I want to enqueue script only on subcategories but on the parent. So lets say I have a parent category 'fun' which has several sub categories like 'stories', 'arts', 'crafts' . How do I load script only for the subcategory but not for parent 'fun'?

Thanks

share|improve this question

2 Answers 2

You can check if a category has a parent, if it does it is a child category.

if( is_category() ){
    $this_category = get_queried_object();
    if( 0 != $this_category->parent ){
        echo 'category has parent';
    }
}
share|improve this answer

[Unlike is_category()] in_category() tests only against a post's assigned categories, not ancestors (parents) of those categories.

Testing if a post is in a descendant category

So, in_category is probably what you are after.

Based on the example cited in your question, you'd do something like this:

if( in_category( array( 'stories', 'arts', 'crafts' ) ) ) {

    // Do what you want...

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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