How to List Categories along with Their Posts

To 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 (+)

  1. söve says:

    very nice job.Thanks

  2. Dave House says:

    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!

  3. 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 post

    Category Sports

    My new post FF Check
    dddddaaang
    my tset new post

  4. Max says:

    Thank you very much for the code!

  5. Blake Moore says:

    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

  6. Dude says:

    Love you! Spent million years for this code! Thank you very very much!

  7. Eduardo says:

    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!

    • Nappa says:

      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().

  8. You just saved me a whole load of time, thanks so much

  9. Ken says:

    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!

  10. lara_lara says:

    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.

  11. Rachel says:

    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!

  12. Nadiar says:

    Thanks Reza, this code work very well,

    I have a question, how do I exclude specific categories?

    Thanks

  13. Ankara Sove says:

    This code will definetely save me time. Thanks dude

  14. Wow, it really work ! thanks a lot !

  15. Aniruddh says:

    Thank you so much for the code :) . Can I use this code for the tags also? Please advise. Thanks.

  16. Dian says:

    Got it!, It is work… nice share guys…

  17. Aditya says:

    Can I list posts under categories in drop down from in wordpress.com (rather than .org?)

Trackbacks/Pingbacks

  1. How to List Categories along with Their Posts
  2. 70 Excellent WordPress Tips And Tutorials From First Three Months Of 2011 | stylishwebdesigner
  3. نمایش مطالب یک دسته خاص به صورت کشویی | قالب , پلاگین و آموزش وردپرس
  4. پرتال جامع و خودکار تیزر دات کو » آرشیو » نمایش مطالب یک دسته خاص به صورت کشویی
  5. How to Make Sitemap Template.
  6. نمایش مطالب یک دسته خاص به صورت کشویی
  7. 30 useful wordpress tipsDesign Freebies

Leave a Reply

 

Amazingly Beautiful WordPress Themes