Sorry for the complicated title.
I am working with this code, to display my sub child categorys with posts under them. The problem is that the code is displaying ALL Sub child categorys (+ posts) that I have on the site. I only want to show the Sub categorys that have connection with the post. All Sub child categorys under Child one have connection with the posts, so you can say I want to display the sub child categorys under child on becuse the post is related with child on.
Category structure (Year is in the title):
- G
- Child 1
- Gaming Year 1
- Post for here
- Gaming Year 2
- Post for here
- Gaming Year 1
- Child 1
My code:
<?php
$cat_id = get_query_var( 'cat' );
$subcats = get_categories( 'child_of=' . $cat_id ); // child categories
class Cat_Walker extends Walker_Category {
function end_el( &$output, $page, $depth = 0, $args = array() ) {
$posts = get_posts( 'cat=' . $page->term_id );
if ( sizeof( $posts ) > 0 ) {
$output .= '<ul>';
foreach ( $posts as $post ) {
$output .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $post->ID ), $post->post_title );
}
$output .= '</ul>';
}
$output .= '</li>';
}
}
foreach ( $subcats as $subcat ) {
$subsubcats = get_categories( 'child_of=' . $subcat->term_id ); // sub child categories
foreach ( $subsubcats as $subsubcat ) {
$args = array(
'title_li' => '',
'show_option_none' => '',
'taxonomy' => 'category',
'child_of' => $subsubcat->term_id,
'walker' => new Cat_Walker( )
);
wp_list_categories( $args );
}
}
?>
Any ideas?
Thanks in advance!