$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How to get child category list post in one template?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - How to get child category list post in one template?

matteradmin12PV0评论

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?

Thanks

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?

Thanks

Share Improve this question asked May 15, 2011 at 3:02 idontknowhowidontknowhow 3152 gold badges4 silver badges20 bronze badges 1
  • 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 Commented May 15, 2011 at 9:34
Add a comment  | 

3 Answers 3

Reset to default 3

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;                
        endif; 

    endforeach; ?>

Parent child taxonomy loop

$taxonomy = 'product_cat';
$category = get_terms($taxonomy);

echo '<ul>';

foreach( $category as $cat ){
    $link = get_term_link( $cat->slug, $taxonomy );

    if(empty($cat->parent)){
        echo '<li><a href="' . $link . '">' . $cat->name . '</a>' . '<strong>' . $cat->count . '</strong></li>';

    }
    $loop = 0;

    foreach( $category as $par ){
        $link = get_term_link( $par->slug, $taxonomy );
        if($cat->term_id == $par->parent ){

        if($loop == 0){ echo '<ul>'; }
        echo '<li><a href="' . $link . '">' . $par->name . '</a>' . $par->count . '</li>';

        $loop++;
        }
    }
    if($loop > 0){ echo '</ul>'; }

}

echo '</ul>';
<ul class="catTags">
<?php
        $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 );
    ?>
    </ul>   
    </div>

    <?php get_sidebar(); ?>

And the CSS :

section .primary ul.catTags {
 padding:0px;
}

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 {
 font-size:18px;
 text-decoration:underline;
}
section .primary ul.catTags li a:hover {
 text-decoration:none;
}
section .primary ul.catTags li ul.children {
 max-width:200px;
}
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 {
 font-size:14px;
 text-decoration:none;
}
section .primary ul.catTags li ul.children li a:hover {
 text-decoration:underline;
}
Post a comment

comment list (0)

  1. No comments so far