WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display List of Current Category's Children OR Posts (18 posts)

  1. Jeremy
    Member
    Posted 5 years ago #

    I'm developing a theme for someone, and they need a lot of category navigation. The goal is for each parent category page to display a list of its children categories. However, if we're at the deepest categories where actual content begins, it should display posts instead of categories.

    So essentially I want to tell WordPress' archive.php page that if is_category() and the category has children, display the kiddies. If it doesn't, display posts.

    How do I tell WordPress to do that?

  2. stvwlf
    Member
    Posted 5 years ago #

  3. Jeremy
    Member
    Posted 5 years ago #

    I can't seem to pass the current category's ID to the child_of parameter. I'm trying with wp_list_categories right now for simplicity. Is there no way to pass a variable into the function's parameters?

    <?php wp_list_categories('child_of=$cat-id') ?>
  4. Jeremy
    Member
    Posted 5 years ago #

    I found some other Codex material that wrote it like this:

    <?php get_categories('child_of='.$post->ID.') ?>

    This way doesn't work with wp_list_categories either. Maybe I'm just not familiar enough with PHP to make good sense out of get_categories. All the examples I see seem to employ global variables I don't know.

  5. gravitythedon
    Member
    Posted 5 years ago #

    This would answer my question as well, could anyone help with this please?

  6. Jeremy
    Member
    Posted 5 years ago #

    I solved this problem, so I thought I'd let folks know.

    Inside the Loop, where it says:

    <?php while(have_posts()) : the_post(); ?>

    you can add a line right under it, like this:

    <?php while(have_posts()) : the_post(); ?>
    
    <?php if(is_category() && in_category($cat)) { ?>

    Then at the end of the Loop you need to change:

    <?php endwhile; ?>

    to this:

    <?php } endwhile; ?>

    My only problem now is, even though the Loop isn't bringing up posts on parent category archives, it still shows the Previous Posts links. I'm not sure how to hide those...

  7. jacobfriedman
    Member
    Posted 4 years ago #

    Check out wp-includes/classes.php. Here's something I tossed into Walker::start_el():

    $mycat=$category->term_id;
    			$anychildren=get_categories('child_of='.$mycat);
    			if (!$anychildren) { $postquery = query_posts('cat=' . $category->term_id .'\''); 
    
    				$endnodelist .= '<ul>';
    				if ( have_posts() ) : while ( have_posts() ) : the_post();
    				$endnodelist .= '<li>' . get_the_title() . '</li>';
    				endwhile;
    				endif;
    				$endnodelist .= '</ul>';
    
    			}

    And finally

    $link .= $cat_name .'</a>' . $endnodelist;.

    Hope it helps - enjoy!

    Cheers
    Jacob

  8. Chad
    Member
    Posted 4 years ago #

    I'm still having a bit of trouble with this one myself... :)

    grabbing the current category and listing only it's children is easy enough for me to do using:

    $cat_id = get_query_var('cat');
    
    wp_list_categories('orderby=id&title_li=&child_of=' . $cat_id);

    But I'm still trying to work out the necessary if statement to list the children if they exist, or display the loop if they don't.

    Any help would be appreciated... I must be missing something simple here.

  9. MichaelH
    Member
    Posted 4 years ago #

    Use the echo argument with the template tag, wp_list_categories():

    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id);
    
    if ($catlist) {
    echo $catlist:
    } else {
    // do your loop stuff
    }
  10. Chad
    Member
    Posted 4 years ago #

    Thanks for replying Michael.

    Ugh. I usually pride myself on being the kind of guy who only need things explained once (at least when dealing with code, anyway ;) ) but apparently I'm a bit thick headed today...

    Tried everything I can think of with your suggestion but to no avail.

    Where (in reference to the start of the loop) should your code be placed... and when you comment 'do your loop stuff' do you mean I should be placing the beginning of the loop into the curlies, or the entire contents of the loop?

    Like I said, I don't know why I'm having such a hard time with this... it seems like it should be obvious.

  11. MichaelH
    Member
    Posted 4 years ago #

    That's meant to work in Category Templates. Use the Template Hierarchy to determine what Template your theme uses to display category archives.

  12. Chad
    Member
    Posted 4 years ago #

    I'm familiar with the hierarchy :) I'm editing category.php. I can the list of children to echo out using the variable structure you recommended, but I can't make it conditional.

    If I try to include the Else statement you demonstrate I wind up with various parse errors, usually referring to the ':' after the echo command, if I remove/ edit that it has an issue with some of the curly braces.

    If I run just the If statement without the Else, it echos out children, but on categories that don't have children, it echos out "No categories."

    Sorry!

  13. MichaelH
    Member
    Posted 4 years ago #

    Please paste all the code from the theme template file (probably index.php) that is displaying those posts into a pastebin such as wordpress.pastebin.ca, and report the link back here. Maybe someone can spot your problem. Thanks.

  14. Chad
    Member
    Posted 4 years ago #

    Here's one (of many) variations I've tried in my category.php: http://wordpress.pastebin.ca/1675833

    The code I'm trying to add in starts on line eleven. This obviously doesn't achieve the end result of only displaying the loop when there are no children for the category being displayed.

    Right now I'm just trying to verify that the if statement is working by echoing out a different string when there are no children to display, unfortunately it isn't working, as I described in my previous post.

    Once that works I'll try removing that string and running the loop as my "Else."

    Any suggestions anyone can offer up would be greatly appreciated.

    PS, this is part of the PressPlay theme - in case that helps.

    Thanks again!

  15. MichaelH
    Member
    Posted 4 years ago #

    Okay this code if for a category.php template and if there are child categories of the current category it displays those categories otherwise it display posts of that category.

    http://wordpress.pastebin.ca/1675954

  16. Chad
    Member
    Posted 4 years ago #

    <?php lightbulb($think_outside_the_box) ?>

    Thanks Michael. I knew I was over complicating it. I got so hung up on sticking with the code you originally posted that using get_categories to populate $catlist and then wp_list_categories instead of trying to echo out $catlist never dawned on me.

    The only change I had to make to that code was to remove the "echo=0" so the lists would actually display... after that it was perfect!

    Thanks for taking the time to get that working (and for commenting all of the code blocks more than the theme designer did ;)

  17. groomedmonkey
    Member
    Posted 4 years ago #

    This is great and I am using the snippet provided above:

    <?php // list child categories
    $cat_id = get_query_var('cat');
    
    $catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id);
    
    if ($catlist) {
    echo $catlist;
    } else {
    echo "Please don't print No categories";
    } ?>

    But if you go to a category without child categories it prints "No categories" which is a bit lame. Mainly because I can't work out how to intercept that output. Any ideas?

  18. frescova
    Member
    Posted 4 years ago #

    Thanks groomedmonkey;

    How about if you want to list the "sibling" categories?

    The code works great when I'm viewing the parent category, but as soon as I navogate to one of the children the list goes away; Ideally the list remains with the current category being viewed not being displayed in the list of siblings.

    Make sense?

Topic Closed

This topic has been closed to new replies.

About this Topic