0

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 let's say I have a parent category 'fun' which has several sub categories like 'stories', 'arts', 'crafts' . How do I load a script only for the subcategory but not for parent 'fun'?

Thanks

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';
    }
}
0

[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...

}

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.