Archives listing posts by category


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

This used to be code adapted from Z'Oc, but I decided to start again and write it fresh with WP_Query instead of get_posts.


Copy this code and paste it in your HTML
  1. function custom_archives_template() {
  2. $cats = get_categories("hierarchical=0");
  3. if ($cats) :
  4. echo '<h2>By Category:</h2>';
  5. foreach ($cats as $cat) :
  6. $cat_name = $cat->cat_name;
  7. $cat_id = $cat->cat_ID;
  8. $cat_link = get_category_link($cat_id);
  9.  
  10. echo '<ul>';
  11. echo ' <li class="cat_name"><h3>' . $cat_name . ' <span class="count">' . $cat->category_count . '</span></h3>';
  12. $catposts = new WP_Query("showposts=-1&cat=$cat_id");
  13. if ($catposts) :
  14. echo ' <ul>';
  15. while ($catposts->have_posts()) : $catposts->the_post();
  16. echo ' <li><a href="' . get_permalink() . '">' . get_the_title() . '</a> <span>' . get_the_time('j F Y') . '</span></li>';
  17. endwhile;
  18. echo ' </ul>';
  19. endif;
  20. echo ' </li>';
  21. echo '</ul>';
  22.  
  23. endforeach;
  24. endif;
  25. }
  26. remove_action('thesis_hook_archives_template', 'thesis_archives_template');
  27. add_action('thesis_hook_archives_template', 'custom_archives_template');

URL: http://www.z-oc.com/blog/2008/03/category-based-archive/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.