Listing child categories and posts
-
Hi
In my sidebar I would like to have a list of all the child categories and any posts under those child categories of a specific parent category. Here is an example:
ARTICLES
-
Brand
- Brand consistency
- Printing colour
- Paper
Brand something else
Printing
The parent category is: Site Articles
I have found two pieces of code that do part of what I want but I am not sure how to integrate the two.
This code displays all categories.
<?php //for each category, show posts $cat_args=array( 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'orderby' => 'title', 'order' => 'ASC', 'showposts' => -1, 'category__in' => array($category->term_id), 'category__not_in' => array(4,5,6, 10), 'caller_get_posts'=>1 ); $posts=get_posts($args); if ($posts) { echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name. '</a> '; foreach($posts as $post) { setup_postdata($post); ?> <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul></li> <?php } // foreach($posts } // if ($posts } // foreach($categories ?>
This code displays the posts from the parent category:
<?php //get all posts for children of category $cata $cata = 16; $taxonomy = 'category'; $cata_children = get_term_children( $cata, $taxonomy ); $args=array( 'category__in' => $cata_children, 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo 'List of Posts belonging to Category A children'; while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; } wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I would really appreciate if if someone could help me out by showing me what code I need to do this?
Thank you so much.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Listing child categories and posts’ is closed to new replies.