This tutorial will help you build a ‘Latest Posts by Category Archive‘ in a very easy way. The widths in the CSS styling presented below have been calculated based on the default WordPress theme, assuming that is the most common theme available to anyone.
If you are looking for a plugin to generate such an archive, please check out: WP Plugin: Latest Posts by Category Archive.
Setting up the page template
Open up you favorite code editor and create a blank document. Save the document as ‘category-archive.php’ (or any other name you’d prefer) in the default WordPress theme directory (wp-content/themes/default).The first step is to asign our new template a name and a page-like structure, so based on the default theme’s page template, the code you should paste in your new document is:
01.<?php02./*03.Template Name: Category Archive04.*/05.?>06. 07.<?php get_header(); ?>08. 09.<div id="content" class="narrowcolumn">10. 11. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>12. <div class="post" id="post-<?php the_ID(); ?>">13. <h2><?php the_title(); ?></h2>14. <div class="entry">15. <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>16. 17. <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>18. 19. </div>20. </div>21. <?php endwhile; endif; ?>22. 23. <!-- Category Archive Start -->24. <!-- Category Archive End -->25. 26.</div>27. 28.<?php get_sidebar(); ?>29. 30.<?php get_footer(); ?>Adding the archive’s PHP code
Simply put, the code below will cycle through the first-level categories of your blog (parent categories), check for the ones that are not empty and if this condition is met, return an unordered list of the latest 5 post from each category. Empty categories will not be displayed.01.<?php02./*03.Template Name: Category Archive04.*/05.?>06. 07.<?php get_header(); ?>08. 09.<div id="content" class="narrowcolumn">10. 11. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>12. <div class="post" id="post-<?php the_ID(); ?>">13. <h2><?php the_title(); ?></h2>14. <div class="entry">15. <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>16. 17. <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>18. 19. </div>20. </div>21. <?php endwhile; endif; ?>22. 23. <!-- Category Archive Start -->24. <ul class="catArchive">25. <?php26. $catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");27. 28. $catCounter = 0;29. 30. foreach ($catQuery as $category) {31. 32. $catCounter++;33. 34. $catStyle = '';35. if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';36. 37. $catLink = get_category_link($category->term_id);38. 39. echo '<li'.$catStyle.'><h3><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h3>';40. echo '<ul>';41. 42. query_posts('cat='.$category->term_id.'&showposts=5');?>43. 44. <?php while (have_posts()) : the_post(); ?>45. <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>46. <?php endwhile; ?>47. 48. <li><a href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <strong><?php echo $category->name; ?></strong></a></li>49. </ul>50. </li>51. <?php } ?>52. </ul>53. <!-- Category Archive End -->54. 55.</div>56. 57.<?php get_sidebar(); ?>58. 59.<?php get_footer(); ?>The PHP code explained
The first thing we do is to set up a database query to cycle through the non-empty parent categories:1.$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");Let’s say that you’d like the archive to exclude the categories with IDs 2, 5 and 6. Your query would become:
1.$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0 AND wterms.term_id NOT IN (2,5,6)");1.$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0 AND wterms.term_id IN (2,5,6)");Next, we set up a category counter which will be incremented each time a category will be displayed. Based on this counter, the code adds a ‘catAlt‘ class that you can use to style differently consecutive categories. In our case, we’ll use it to shift the categories into two columns. This is were the alternate classes are assigned:
1.$catCounter++;2. 3.$catStyle = '';4.if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';1.foreach ($catQuery as $category) {2. /* Code used to retrieve and display the latest posts */3.}1.echo '<li'.$catStyle.'><h3><a href="'.get_category_link($category->term_id).'" title="'.$category->name.'">'.$category->name.'</a></h3>';1.query_posts('cat='.$category->term_id.'&showposts=5');?>2. 3.<?php while (have_posts()) : the_post(); ?>4. <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>5.<?php endwhile; ?>6. <li><a href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <strong><?php echo $category->name; ?></strong></a></li>Styling the archive
Find the style.css file in your default theme’s directory, open it and scroll down to the end of the file. Now copy the code below and paste it in your file.01./* Latest posts by category styles */02. 03..catArchive {04. width: 450px;05. overflow: hidden;06. margin: 20px 0 0 0;07. padding: 0;08. list-style-type: none;09.}10. 11..catArchive h3 {12. font: normal bold 18px sans-serif;13. border-bottom: 1px solid #666;14. margin: 0;15. padding: 0 0 3px 0;16. display: block;17.}18. 19..catArchive li {20. display: block;21. float: left;22. width: 210px;23. margin: 0 30px 30px 0;24.}25. 26..catArchive ul {27. margin: 0;28. padding: 0;29.}30. 31..catArchive li li {32. border-bottom: 1px solid #ddd;33. margin: 0;34. padding: 4px 0;35.}36. 37..catAlt {38. margin-right: 0 !important;39.}Note: Keep in mind that column sizes have been calculated based on the available space (450 pixels) in the default theme’s content area. So, our columns should each be 210 pixels wide, with a 30 pixels spacing between them.
Since the column shifting is done with the help of float, I’ve set the overflow to hidden to stretch the list full height. This can only work with a fixed given container width, which in our case is 450px set on .catArchive.
If your new to “floating”, Sarah has posted a fairly easy to understand article about the float property on BloggingTips.com.
You can download an archive containing both source files: Download.
Comments
0 Response to 'WordPress How To: Latest Posts by Category Archive'
Post a Comment