Listing Parent, Child and GrandChild Categories and then the PostTitles on Page Template !

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

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

Leave a Comment