List WordPress Posts by Category


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /* template name: Posts by Category! */
  3. get_header(); ?>
  4.  
  5. <div id="container">
  6. <div id="content" role="main">
  7.  
  8. <?php
  9. // get all the categories from the database
  10. $cats = get_categories();
  11.  
  12. // loop through the categries
  13. foreach ($cats as $cat) {
  14. // setup the cateogory ID
  15. $cat_id= $cat->term_id;
  16. // Make a header for the cateogry
  17. echo "<h2>".$cat->name."</h2>";
  18. // create a custom wordpress query
  19. query_posts("cat=$cat_id&post_per_page=100");
  20. // start the wordpress loop!
  21. if (have_posts()) : while (have_posts()) : the_post(); ?>
  22.  
  23. <?php // create our link now that the post is setup ?>
  24. <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
  25. <?php echo '<hr/>'; ?>
  26.  
  27. <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
  28. <?php } // done the foreach statement ?>
  29.  
  30. </div><!-- #content -->
  31. </div><!-- #container -->
  32.  
  33. <?php get_sidebar(); ?>
  34. <?php get_footer(); ?>

URL: http://wesbos.com/wordpress-list-posts-by-category/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.