WordPress - Latest posts by categoryOne 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]

Read the rest of this page »

‘); ?>

Pages: ‘, ‘after’ => ‘

‘, ‘next_or_number’ => ‘number’)); ?>



[/php]

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.

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.

[php]

Read the rest of this page »

‘); ?>

Pages: ‘, ‘after’ => ‘

‘, ‘next_or_number’ => ‘number’)); ?>

    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″);

    $catCounter = 0;

    foreach ($catQuery as $category) {

    $catCounter++;

    $catStyle = ”;
    if (is_int($catCounter / 2)) $catStyle = ‘ class=”catAlt”‘;

    $catLink = get_category_link($category->term_id);

    echo ‘

    ” rel=”bookmark” title=”“>

  • MySQL manual.

    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:

    [php]
    $catCounter++;

    $catStyle = ”;
    if (is_int($catCounter / 2)) $catStyle = ‘ class=”catAlt”‘;
    [/php]

    The category list is built by the use of foreach and it helps retrieve vital information about the categories, such as name and permalink:

    [php]
    foreach ($catQuery as $category) {
    /* Code used to retrieve and display the latest posts */
    }
    [/php]

    After we display the category title and link through this line of code

    [php]
    echo ‘

    ” rel=”bookmark” title=”“>

56 comments on “WordPress How To: Latest Posts by Category Archive

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>