WordPress - List Sub Categories and Sub-Sub Categories, With Posts
I have the following code. It allows me to show sub categories and documents within them. The thing is, a sub category may contain further sub categories. The code below will show documents belonging to a sub-sub (grand child) category at sub (parent) level.
Please can anyone shed light on how to solve this so document only show underneath the category they are directly under? In addition, highlighting what is a sub-sub (grandchild) category as well? All posts titles should be linked to the post
?php
$categories = get_categories('child_of=34');
foreach ($categories as $category) {
//Display the sub category information using $category values like $category-cat_name
echo 'h3'.$category-name.'/h3';
echo 'ul';
foreach (get_posts('cat='.$category-term_id) as $post) {
setup_postdata( $post );
echo 'lia href="'.get_permalink($post-ID).'"'.get_the_title().'/a/li';
}
echo '/ul';
}
?
So something as follows
- Sub Cat 1
- Post 1
- Post 2
- Sub Cat 2
- Sub Sub Cat 1
- Post 3
- Sub Sub Cat 2
- Post 4
- Sub Sub Cat 1
Topic get-posts categories Wordpress
Category Web