WordPress.org

Ready to get started?Download WordPress

Forums

Conditional Tags for Parent/Child Categories? (18 posts)

  1. grypesagon
    Member
    Posted 8 years ago #

    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?

  2. maerk
    Member
    Posted 8 years ago #

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

  3. grypesagon
    Member
    Posted 8 years ago #

    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.

  4. maerk
    Member
    Posted 8 years ago #

    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.

  5. grypesagon
    Member
    Posted 8 years ago #

    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.

  6. maerk
    Member
    Posted 8 years ago #

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

  7. grypesagon
    Member
    Posted 8 years ago #

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

  8. maerk
    Member
    Posted 8 years ago #

    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:

    http://pastebin.be/949/

    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?

    or

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

  9. grypesagon
    Member
    Posted 8 years ago #

    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.

  10. maerk
    Member
    Posted 8 years ago #

    OK, I have written the conditional tag for you:

    http://pastebin.be/975/

    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!

  11. grypesagon
    Member
    Posted 8 years ago #

    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?

  12. maerk
    Member
    Posted 8 years ago #

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

  13. grypesagon
    Member
    Posted 8 years ago #

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

  14. grypesagon
    Member
    Posted 8 years ago #

    it doesn't seem to be working.

    http://www.grypesagon.com/code/category/personal/

    that's the site I loaded the code into...

    any thoughts?

  15. venta2
    Member
    Posted 8 years ago #

    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.

  16. thekelpie
    Member
    Posted 7 years ago #

    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?

  17. Kafkaesqui

    Posted 7 years ago #

    thekelpie, you may be interested in a plugin I wrote a while back which provides a similar conditional to test, though the difference from maerk's code is it returns true for the category specified whether you're on that category or any of its children.

    Info and link to plugin here:

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

  18. thekelpie
    Member
    Posted 7 years ago #

    Kafkaesqui - thanks for your help. I think I've figured out the required code based on Maerk's method. I'm using the Category Template Inheritor plugin, as I want the parent and children to share certain features not required by other categories (effectively they're forming a kind of directory), and this had me confused for a while.

    On the off-chance that any other PHP newbies need to do the same thing, here's the code I ended up using in my category template (between the 'navigation' divs). (Kubrick theme.) There are no doubt neater ways of doing it. I've tweaked the presentation of the posts for my own use, so anyone else using this will have to adapt it.

    `
    <div class="post">
    <?php if ( cat_is_parent() ) {
    echo wp_list_cats('sort_column=name&child_of=2');
    } else {
    while (have_posts()) : the_post();
    echo '<h3>';
    echo '<a href=';
    echo the_permalink();
    echo '>';
    echo the_title();
    echo '';
    echo '</h3>';
    echo '<div class="entry">';
    echo the_content();
    echo the_meta();
    echo '</div>';
    endwhile;
    }
    ?>
    </div>

Topic Closed

This topic has been closed to new replies.

About this Topic