Support » Fixing WordPress » Display List of Current Category’s Children OR Posts

  • Resolved Jeremy

    (@jeremiahhenson)


    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?

Viewing 15 replies - 1 through 15 (of 17 total)
  • stvwlf

    (@stvwlf)

    Jeremy

    (@jeremiahhenson)

    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') ?>
    Jeremy

    (@jeremiahhenson)

    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.

    gravitythedon

    (@gravitythedon)

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

    Jeremy

    (@jeremiahhenson)

    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…

    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

    Chad

    (@lynneandchad)

    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.

    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
    }
    Chad

    (@lynneandchad)

    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.

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

    Chad

    (@lynneandchad)

    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!

    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.

    Chad

    (@lynneandchad)

    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!

    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

    Chad

    (@lynneandchad)

    <?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 😉

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Display List of Current Category’s Children OR Posts’ is closed to new replies.