Support » Themes and Templates » Month-by-month archive page layout?

Month-by-month archive page layout?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Check out the query_posts function; one way to do what you want is to call it multiple times, specifying different months by passing them as parameters.

    Good to see the function – but I would want to show posts from all months, rather than just specify specific months.

    Right, you would loop through all your months and call that function for each month, then you could do this:

    I’m not certain how to put the while (have posts()) statement for entries inside a similar while have-months sort of loop.

    Sorry, I don’t know how I’d loop through each month.

    And I wonder how I would modify the output if the current archive was a date-based archive rather than a category archive – because displaying “September 2006” was be unnecessary duplication if I was on the September 2006 archive page. It would still be good if it was an annual archive, though. Hmm.

    My apologies. I was assuming too much familiarity with WordPress.

    WordPress has a database object called $wpdb that makes database queries really easy. Declare $wpdb as global somewhere in your archive file, like so:

    global $wpdb;

    Then you can get an array of archive month objects like so:

    $results = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts
    FROM $wpdb->posts WHERE post_date !=
    '0000-00-00 00:00:00' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");

    So that if you loop through the $results array:

    foreach ( (array) $results as $month ) {

    you can get each month, starting with the most recent month, that has posts in it, like so:

    $month->year will return the year number (e.g. 2006)
    $month->month will return the month number (e.g. 9)
    $month->posts will return the number of posts in that month

    So to do what you describe, within the foreach loop above, call the query_posts function, perhaps similar to the following:


    Then after that you can have the normal posts loop, and use the normal loop functions.

    if (have_posts()) : while (have_posts()) : the_post();


    I have so far managed to get to here – http://pastebin.com/792383

    This has a number of issues…
    * No name for the month
    * Can’t seem to paginate archive pages as is the default behaviour (?).
    * Instead, I see the complete top-to-bottom listing of posts in this category, unbroken.
    * Using get_permalink around the post title returns the link to the category page, not to the post.
    * This method becomes evident on every type of archive page, including monthly – on monthly archive pages, naturally, we should display only posts from the current month, and no header. In fact, it looks like it would all work more neatly as part of a plugin that simply modifies the_loop when it occurs on an archive page, rather than forcing all this code to be in the archive template. Any takers?

    Okay, I have to to using…

    <?php if (have_posts()) : ?>

    <?php global $wpdb; ?>

    <?php $results = $wpdb->get_results(“SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $wpdb->posts WHERE post_date != ‘0000-00-00 00:00:00’ AND post_status = ‘publish’ GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC”); ?>

    <div class=”entrytext”>

    foreach ( (array) $results as $month ) {
    echo ‘year.’/’.$month->month.’/”>Month Name ‘.$month->year.’‘;

    echo ‘<ul class=”postspermonth”>’;

    while (have_posts()) : the_post();

    echo ‘

  • ‘.get_the_time(‘j’).’: ‘.get_the_title().’
  • ‘;


    echo ”;


    This is currently on my index page. However, it lacks a few things I need…

    1. Using the actual name of the month, not just $month->month
    2. Control over how much I display (ie. on the front page index, maybe I just want to display that latest 12 headlines, whichever month, or maybe I want to display posts from the last three months (actually I don’t; it’s the “last 12” thing I want).
    3. Pagination – so how can this work on archive pages, say, where the list of posts is too long and I want to break it across multiple pages using regular pagination. No idea on how to tackle that.

    4. This has the side effect of somehow giving the page focus as n monthly archive. I’m using it on my front page index.php, as I said – but some conditional sidebar code elsewhere that should only be displayed by “elseif (is_month())” – is actually being displayed.

    Any ideas please?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Month-by-month archive page layout?’ is closed to new replies.