WordPress How To: Latest Posts by Category Archive
One technique that is most common with WordPress magazine or news style themes is the display of an archive of the latest posts by category, as simple titles or with post excerpts. This is useful for the previously mentioned theme styles, but not only. It can be used to set up custom blog homepages, 404 pages, landing pages or even a special archive page.
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:
< ?php
/*
Template Name: Category Archive
*/
?>
< ?php get_header(); ?>
<div id="content" class="narrowcolumn">
< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2>< ?php the_title(); ?></h2>
<div class="entry">
< ?php the_content('<p class="serif">Read the rest of this page »'); ?>
< ?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
< ?php endwhile; endif; ?>
<!-- Category Archive Start -->
<!-- Category Archive End -->
</div>
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>
The template above will make sure to display the page name you set up, and also, any additional content you might want to add before the archive, from you WordPress page editor. We will be adding our ‘latest posts by category’ code between the ‘Category Archive Start’ and ‘Category Archive End’ comments.
Click here to read the full blog post!
Post Excerpts and the More Tag in WordPress
Not only once, it has been said that displaying full posts on your blog home page, category pages or archive pages increases the chance of being penalized for duplicate content.
In WordPress, using post excerpts has proven to be a good alternative, but one that has its downsides:
- You lose control over text formatting;
- Images won’t be displayed;
- If you don’t take time to write them yourself, WordPress might not select the most appropriate fragments;
- Readers might not be convinced to further click, if the excerpt is not attractive enough.
Better Post Excerpts
If you’re concerned about your writing style, you’ll most definitely take care about how you structure your posts, where you insert images and how you write the introductory paragraph.
With these in mind, you basically got yourself a very good post excerpt, one that you should not leave to WordPress to decide when and where to cut.
The ‘More’ Tag
This is the most valid alternative to displaying post excerpts, if the_excerpt() template tag does not fit your needs.
More is what the WordPress developers call a quicktag, designed to cut-off large posts into two fragments: one that will be displayed as an excerpt and one that users will continue to read from after clicking the “read more” link. It serves as a marker inside the post so that users who come from the excerpt link, will start reading the content from that point on, and not from the beginning, again.
Click here to read the full blog post!
WordPress Template Tags: wp_get_archives()
Welcome to our new series’ first post! The “WordPress Template Tags” series is intended to bring into spotlight some of WordPress’ less common, but very useful template tags. While the contents can’t be considered a revelation to advanced WP users and developers, it can be of big help to those who take their first steps in the amazing world of online publishing using this famous blogging platform.
But enough chit chat! Here’s our first recommendation:
wp_get_archives()
Have you ever thought of how to display a list of your most recent posts? Or a list of all posts published in the last 10 days? Or 6 months? Or 2 years?
Click here to read the full blog post!
