WordPress.org

Ready to get started?Download WordPress

Forums

Distinguish between a parent category and child category page (7 posts)

  1. chugs
    Member
    Posted 6 years ago #

    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

  2. Kafkaesqui

    Posted 6 years ago #

    Well there's this:

    http://wordpress.org/support/topic/69002#post-363703

    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:

    <?php
    $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; ?>
  3. chugs
    Member
    Posted 6 years ago #

    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 (http://wordpress.org/support/topic/146644?replies=2#post-659205) but it doesn't seem to work.

  4. Kafkaesqui

    Posted 6 years ago #

    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:

    http://wordpress.pastebin.ca/900941

  5. chugs
    Member
    Posted 6 years ago #

    Thanks. I'll give it a try.

  6. ghop2003
    Member
    Posted 5 years ago #

    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.

  7. mores
    Member
    Posted 5 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.