• Re-posted from plugins (no replies)

    I have not been able to find this among the existing plugins. If it is possible without a plugin, please let me know!

    Description:

    Normally each post will include a date, say month and day and time. Post #1 would have month, day, time… post #2 would have month, day, time… etc.

    Example:

    NOVEMBER 8, 12:31 pm
    NOVEMBER 12, 5:56 am
    NOVEMBER 18, 1:13 pm

    Request:

    I would like to group my dates so that I get a Month heading, then a day for each post. If there are multiple posts on a day there would be times under a day heading as well.

    Example:

    NOVEMBER
    1
    4
    6
    8
    – 12:31 pm
    – 5:21 pm
    – 11:19 pm
    13
    19
    21
    – 1:14 am
    – 6:41 pm
    26
    31

    DECEMBER
    2
    12
    22
    29

    This is primarily a display issue since it doesn’t appear that much has to happen beyond the interface… but it is above my coding capabilities.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The basic idea is to keep track of the date information, then only display it when it changes.

    For example, let’s say this is your Loop:

    if ( have_posts() ) : while ( have_posts() ) : the_post();
    // stuff to display the post here
    endwhile;

    Now, you want to only display the month when it changes. This means you need to keep track of it through multiple iterations of the loop. So first we initialize it before the Loop, then get it every time through, only echo’ing it when it changes.

    $oldmonth = ''; // start off blank
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    $newmonth = get_the_time('F'); // F is the code for full month name
    if ($newmonth != $oldmonth) {
      echo $newmonth; // show the month name
      $oldmonth = $newmonth; // set the month for the next loop through
    }
    // stuff to display the post here
    endwhile;

    As you see, when the month changes between posts, it gets output, but only then. Similar functionality can be done for day and time as well.

    Thread Starter joelmarsh

    (@joelmarsh)

    Great! Thanks. That makes a lot of sense and seems simple now that you say it.

    Am I correct in thinking that I could do the same for day and year? (please correct my syntax if this is wrong)

    if ($newday != $oldday) {
      echo $newday; // show the day name
      $oldday = $newday; // set the day for the next loop through
    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Same basic idea will work for those, yes.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Date Grouping’ is closed to new replies.