WordPress.org

Ready to get started?Download WordPress

Forums

Month-by-month archive page layout? (8 posts)

  1. parakeet
    Member
    Posted 7 years ago #

    How can I edit my archive pages (in particular, category archives) to present post listings month-by-month? That is,...

    September 2006
    * 11: Title of entry, hello world
    * 08: Title of entry, hello world
    August 2006
    * 23: Title of entry, hello world
    * 08: Title of entry, hello world

    That is, basically the same layout as the Clean Archives plugin (http://www.sporadicnonsense.com/2006/05/04/clean-archives-20/).

    I'm currently running an edited version of the I Haev Style theme (http://www.minid.net/2005/08/29/i-haev-styel-tema-para-wordpress-15x/), which presents archives in a tabular format (http://www.minid.net/2006/08/).

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

    Thank-you

  2. Austin Matzko
    Member
    Posted 7 years ago #

    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.

  3. parakeet
    Member
    Posted 7 years ago #

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

  4. Austin Matzko
    Member
    Posted 7 years ago #

    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.

  5. parakeet
    Member
    Posted 7 years ago #

    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.

  6. Austin Matzko
    Member
    Posted 7 years ago #

    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:

    query_posts("year=$month->year&monthnum=$month->month")

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

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

    etc.

  7. parakeet
    Member
    Posted 7 years ago #

    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?

  8. parakeet
    Member
    Posted 7 years ago #

    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">

    <?php
    foreach ( (array) $results as $month ) {
    query_posts("year=$month->year&monthnum=$month->month");
    echo 'year.'/'.$month->month.'/">Month Name '.$month->year.'';

    echo '<ul class="postspermonth">';

    while (have_posts()) : the_post();

    echo '

  9. '.get_the_time('j').': '.get_the_title().'
  10. ';

    endwhile;

    echo '';
    }
    ?>

    </div>

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic