WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is what I want to have

My category Structure in WP admin

Cat Parent
  -Cat Child 1
  -Cat Child 2
  -Cat Child 3
  -Cat Child 1

This what I want to have in one template:

It show the list of post in Cat Parent Children Category

Cat Child 1
 -Post1 Child 1
 -Post2 Child 1
 -Post3 Child 1
 -So on... .

Cat Child 2
 -Post1 Child 2
 -Post2 Child 2
 -Post3 Child 2
 -So on... .

Cat Child 3
 -Post1 Child 3
 -Post2 Child 3
 -Post3 Child 3
 -So on... .

Does anyone have done it before or anyone have an idea on how to code this?


share|improve this question
May be I didn't understand your question properly. You want to list posts from child categories when you go to the parent category listing? is this what you are asking for? If yes then this is actually what happens in wordpress already. Or may be you want the posts to be listed in order of child categories? – Hameedullah Khan May 15 '11 at 9:34
up vote 2 down vote accepted

get the child categories, using get_categories(); then loop through them with a foreach loop, using WP_Query() :

<?php  $cats = get_categories('child_of='.get_query_var('cat')); 

    foreach ($cats as $cat) :

    $args = array(
    'posts_per_page' => 3, // max number of post per category
    'category__in' => array($cat->term_id)
    $my_query = new WP_Query($args); 

        if ($my_query->have_posts()) : 
        echo '<h3>'.$cat->name.'</h3>';

        while ($my_query->have_posts()) : $my_query->the_post(); ?>     
        <?php /*general loop output; for instance: */ ?>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>    <br />  

        <?php endwhile; ?>

        <?php else : 
        echo 'No Posts for '.$cat->name;                

    endforeach; ?>
share|improve this answer
wp_reset_query usage is not correct, it is used to reset the main loop query, but here you have created a second query and calling wp_reset_query will have no effect on your $my_query. – Hameedullah Khan May 15 '11 at 9:35
@Hameedullah Khan thanks for the comment; i edited it out from the above code. if needed, some code to preserve the original query has to be added to the above code. – Michael May 15 '11 at 11:05
Awsome!! really work great!! you help me a lot Thank you so much Michael!! – idontknowhow May 16 '11 at 14:11
<ul class="catTags">
        $args = array(
        'show_option_all'    => '',
        'orderby'            => 'count',
        'order'              => 'DESC',
        'style'              => 'list',
        'show_count'         => 0,
        'hide_empty'         => 1,
        'use_desc_for_title' => 1,
        'child_of'           => 0,
        'feed'               => '',
        'feed_type'          => '',
        'feed_image'         => '',
        'exclude'            => 1,
        'exclude_tree'       => '',
        'include'            => '',
        'hierarchical'       => 1,
        'title_li'           => __( '' ),
        'show_option_none'   => __('No categories'),
        'number'             => null,
        'echo'               => 1,
        'depth'              => 2,
        'current_category'   => 0,
        'pad_counts'         => 0,
        'taxonomy'           => 'category',
        'walker'             => null    

        wp_list_categories( $args );

    <?php get_sidebar(); ?>

And the CSS :

section .primary ul.catTags {

section .primary ul.catTags li {
 float: left;
 list-style-type: none;
 margin: 0px 20px 10px 20px;


section .primary ul.catTags li a,section .primary ul.catTags li a:visited {
section .primary ul.catTags li a:hover {
section .primary ul.catTags li ul.children {
section .primary ul.catTags li ul.children li {
 margin: 0px 5px 3px 5px;
 display: inline;
section .primary ul.catTags li ul.children li a, section .primary ul.catTags li ul.children li a:visited {
section .primary ul.catTags li ul.children li a:hover {
share|improve this answer
Please add an explanation to your answer: why could that solve the problem? – toscho May 30 '13 at 7:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.