Support » Themes and Templates » Conditional Tags for Parent/Child Categories?

  • I only have two levels of Categories. Parent and Child. I have no child of child or anything complicated. All posts are assigned to only one child category. Never to the parent. I’m trying to alter my category.php to adhere to the following logic.

    “If this is a parent category list the children of self.
    If this is a child category list the posts within self.”

    is this possible?

Viewing 15 replies - 1 through 15 (of 17 total)
  • Yes, I use something similar on my blog. I wrote some custom functions.

    I’m not sure what you want to do exactly, could you elaborate? Would it be something like what I’ve written below?

    “If this post contains child posts, list those posts.
    If this post belongs in a child category, list the other posts in that category.”

    Sort of. I’m getting a little lost in your use of “post” though.

    my intention is quite literally as follows.

    “if current category is a parent category, list the children of the current category.

    if current category is a child category, list the posts under the current category.”

    basically I want the parent category to display a menu of it’s subcategories without displaying the posts within those categories. I only want the posts to be listed when viewing the child category.

    Ah OK, I think I understand, sorry I thought you were talking about pages with children.

    So you have a structure like this:

    Category 1
    -subcat1 with posts
    -subcat2 with posts
    Category 2
    -subcat3 with posts
    -subcat4 with posts

    That shouldn’t be too difficult, I’ll take a look at it when I get back home.

    Yeah, that’s exactly it. I didn’t think it would be that difficult either. I just can’t seem to work aroudn it though. Thanks for your help.

    I don’t think there’s a conditional tag that covers what you want to do, so we’ll have to concoct our own, but it’s not too hard 😉

    I take it you didn’t have any luck with it last night?

    Yeah, sorry :/ it turns out it’s not a simple as I was expecting. I will take a look at it, but I’m quite busy at the moment so it might be a little while.

    In the meantime, in case you’ve made any forays into PHP already, I’ll post you what I have for is_child() — a function I wrote which checks whether one page is the child of another, specified page. It won’t work on categories, but it might give you a few ideas:

    Put the ID of the parent page to test in the first argument. You can also optionally supply the child’s ID if you want to force it to check a specific ID:

    is_child(645) — is this a child of 645?


    is_child(101, 167) — is 167 a child of 101?

    I’ll take a look at it again over the next few days and see what I can come up with. Sounds like a useful plugin too, so watch this space 🙂

    yeah i’m not that PHP savvy. I’m only good enough to understand what to move where to get what effect. What to remove to make things go away, and if I’m very very very lucky… sometimes… just sometimes… I can figure out what to put in to make something appear. 🙂 Take your time though. I’m looking into other work arounds for the time being.

    OK, I have written the conditional tag for you:

    Copy that into a file called functions.php which should be uploaded to your theme directory. If functions.php already exists in your theme directory, just add it to the end of the file.

    Then you can use it like this:

    if ( cat_is_parent() ) {

    echo 'This category is a parent';
    // Replace this with the code
    // you want when the category is a parent

    } else {

    echo 'This category is a child';
    // and this with the code you want
    // when the category is a child


    That should do the trick!

    That’s fantastic. One question though. I have a function.php in the wp-includes folder. I’m making a new one for just this in the theme folder and not adding it to the one in wp-includes right?

    Yeah, make a new one and put it in the same folder as all your other template files.

    that’s what i figured. just makin sure. I’ll give it a shot and let you know how it turns out. Thanks again.

    it doesn’t seem to be working.

    that’s the site I loaded the code into…

    any thoughts?

    Will something like this work in the postmeta inside the loop as well?

    I’m attempting to create a category listing that looks something like this.

    Posted in: Category1 about Category2 because of Category3.

    Just like the original poster all of my categories include subcategories where each are located.

    I’m trying to do exactly the same thing as grypesagon – for one of my parent categories, I want to display a list of the child categories; for the child categories, I want to display the posts.

    I have inserted Maerk’s code into function.php, and have inserted his if ( cat_is_parent() ) { code into my category template, like this:

    <?php if ( cat_is_parent() ) {
    echo wp_list_cats('sort_column=name&child_of=2');
    } else {
    echo 'This category is a child';

    This is working insofar as I get a list of the child categories (wahey!) when I click on the parent category, and when I click on a child I get “This category is a child”. But (being a PHP newby) how do I make it so that posts are displayed when the category is a child? I’ve been poring over this for a few days and can’t figure out how to get it to display the posts instead of “This category is a child” – how to integrate the necessary code into the “else” clause.

    Can anyone please help?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Conditional Tags for Parent/Child Categories?’ is closed to new replies.