Support » Developing with WordPress » Distinguish between a parent category and child category page

  • Is there a way to find out whether I am on a parent category page or a child category page? So that

    – if it is a parent category page
    do this
    – if it is a child category page
    do something else

Viewing 6 replies - 1 through 6 (of 6 total)
  • Well there’s this:

    If you’re only looking to verify in general terms the category one is on is a parent or a child, you could quickly test on the parent property held in the query object of a category query:

    $cat_obj = $wp_query->get_queried_object();
    if( $cat_obj->parent ) :
    ~ this is a child category ~
    <?php else : ?>
    ~ this is a parent category ~
    <?php endif; ?>

    Thanks. This works but my need is a little more complex.
    What I want is

    – if this is a parent category

    Parent Category Title

    Child category 1 Title
    Child category 1 posts

    Child category 2 Title
    Child category 2 posts

    – if this is a child category

    Child category Title
    Child category posts

    I am trying to combine this with one of your solutions from elsewhere ( but it doesn’t seem to work.

    As an aside, I just want to point out how different (and more complete) your follow-up request is. It’s quite an excellent example of how a starting topic post should look. Anyway…

    It’s a bit complicated for the parent category bit since you want to not only collect child categories but their posts as well, then organize them by (child) category. I can provide a quick solution here by looping each child category of posts, but keep in mind on a listing of many child categories this could get bogged down in database queries and you’ll probably want something more clever.

    Here’s a framework for what you’re asking:

    Thanks. I’ll give it a try.

    I realize this is an old topic, but hopefully someone can help with a slightly different request I have.

    I have categories and sub-categories. I have custom list on my homepage that will go to certain categories. I want to create a category page that will display a list of sub-categories followed by all posts that are in the main category. So, I want:

    If main category page:
    – list all sub-categories (without sub-category posts)
    – then loop through posts in the main category

    If sub-category page:
    – Just list all the posts in the sub-category

    It is worth noting that I only have two levels of categories: parent and child. So I might have: PHP > WordPress, but not PHP > WordPress > Themes.

    That’s exactly what I’m attempting to do too.
    Because I realized, sometimes I want things inside child categories, and sometimes in the main category.
    The display of the child categories along with the “sibling” posts would look, at least on my site, pretty awesome!

    Someone help, please 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Distinguish between a parent category and child category page’ is closed to new replies.