How to List Categories along with Their Posts
17th Mar 2011 | Posted by reza | 26 CommentsTo help site visitor finding one or two categories, We can use default widget or wp_list_categories to list the categories. In some case, We want to display the categories along with their posts. Unfortunately, this default widget and function can’t perform that. But this snippet can.
Put the code below in the sidebar or in a page template to create a site map, or wherever You want to display the categories along with their post.
<?php //get all categories then display all posts in each term $taxonomy = 'category'; $param_type = 'category__in'; $term_args=array( 'orderby' => 'name', 'order' => 'ASC' ); $terms = get_terms($taxonomy,$term_args); if ($terms) { foreach( $terms as $term ) { $args=array( "$param_type" => array($term->term_id), 'post_type' => 'post', '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() ) { ?> <div class="category section"> <h3><?php echo 'Category '.$term->name;?></h3> <ul> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> </div> <?php } } } wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Credit
Thanks to MichaelH and Izvarinskij @ WordPress.org
19 Comments to “How to List Categories along with Their Posts”
Add Comments (+)-
Hey there,
Thanks so much for the code, it’s almost exactly what I need! I wonder if you could tell me how to exclude certain categories from the list, though?
Cheers!
-
Friends here is my working that i want to use:
$taxonomy = 'headline';
$param_type = 'headline__in';
$term_args=array(
'orderby' ?> 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'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() ) { ?><?php echo 'Category '.$term?>name;?>
<?php while ($my_query?>have_posts()) : $my_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile;
?><?php }
}
}
wp_reset_query(); // Restore global post data stomped by the_post().but problem is its outpu result:
Category games
My new post FF Check
dddddaaang
my tset new postCategory Sports
My new post FF Check
dddddaaang
my tset new post -
Thank you very much for the code!
-
You have literally saved me an hour or two of my life and i cant thank you enough for that. Great piece of code. Thanks
-
Love you! Spent million years for this code! Thank you very very much!
-
Can I use this code for Custom Post Type? I tried to modify the code, but could get it working… Could someone help, please?
Thanks!-
I found that is well done with ->slug, here is the code
//get all categories then display all posts in each term
$taxonomy = 'capitoli';
$param_type = 'capitoli__in';
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
echo "TERM slug ".$term->slug;
$args=array(
'capitoli' => ($term->slug),
'post_type' => 'tesi',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'name',
'order' => 'ASC'
);$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>name;?>
have_posts()) : $my_query->the_post(); ?>
<a href="" rel="bookmark" title="Permanent Link to "><?php
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().-
Grazie mille, Nappa. This worked perfectly.
-
-
-
You just saved me a whole load of time, thanks so much
-
Thanks for this! Can someone advise how to modify this so as to exclude a category id or name from the generated list?
Thanks again!
-
It really helped me, thanks a tons, i need one more small help, can u tell me how to exclude one category from the list.
-
Hi there,
This code is great, has saved me loads of time already!
Just wondering if it can be modified to link off to the category archive, rather than display the posts?Thanks!
-
Thanks Reza, this code work very well,
I have a question, how do I exclude specific categories?
Thanks
-
This code will definetely save me time. Thanks dude
-
Wow, it really work ! thanks a lot !
-
Thank you so much for the code . Can I use this code for the tags also? Please advise. Thanks.
-
Got it!, It is work… nice share guys…
-
Can I list posts under categories in drop down from in wordpress.com (rather than .org?)
Trackbacks/Pingbacks
- How to List Categories along with Their Posts
- 70 Excellent WordPress Tips And Tutorials From First Three Months Of 2011 | stylishwebdesigner
- نمایش مطالب یک دسته خاص به صورت کشویی | قالب , پلاگین و آموزش وردپرس
- پرتال جامع و خودکار تیزر دات کو » آرشیو » نمایش مطالب یک دسته خاص به صورت کشویی
- How to Make Sitemap Template.
- نمایش مطالب یک دسته خاص به صورت کشویی
- 30 useful wordpress tipsDesign Freebies
very nice job.Thanks