2

What I want to do is a loop like this is something like this I'm really sorry but its hard to format here!

I going to put this short url http://pastebin.com/dEdKzgJz so you can see it better... Can anyone help me with this? Any advice!

Category 1
........Child Category 1.1
...........Sub-Child Category 1.1.1
.................Post Title 1
.................Post Title 2
.................Post Title 3

Category 2
........Child Category 2.1
...........Sub-Child Category 2.1.1
.................Post Title 1
.................Post Title 2
.................Post Title 3

Category 3
........Child Category 3.1
...........Sub-Child Category 3.1.1
.................Post Title 1
.................Post Title 2
.................Post Title 3

1 Answer 1

1

This would be the way I'd do it. Add the following to your functions.php;

class Walker_Category_Posts extends Walker_Category
{
    function start_el( &$output, $category, $depth, $args )
    {
        parent::start_el( $output, $category, $depth, $args );
        if ( $category->parent )
            return $output;

        if ( $posts = get_posts( 'posts_per_page=-1&no_found_rows=1&update_term_cache=0&cat=' . $category->term_id ) ) {
            $output .= '<ul>';
            foreach ( $posts as $post )
                $output .= '<li><a href="' . get_permalink( $post->ID ) . '">' . apply_filters( 'the_title', $post->post_title ) . '</a></li>';
            $output .= '</ul>';
        }
    }
}

And where you'd like to display the list, call;

<?php wp_list_categories(array('walker' => new Walker_Category_Posts)); ?>
1
  • Thanks for the handy function! I've inserted it into my functions.php file and it will display the cats, subcats.. but not the posts within the subcats. So, in essence I just get a listing of my categories and their hierarchy ... any input on why its not showing posts for the subcats??
    – revive
    Commented Sep 14, 2011 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.