Interspersing your post listing with date headers in WordPress

Update: OK, so at least one person wanted it as a plugin, so feel free to download Intermittent Date Headers.

Here’s a code snippet that’s not really worth making into a plugin. It’s designed to put date headers into your main post listing page. As an example, you could end up with:

Heading: March, 2008
Post: dated 15th March
Post: dated 28th March
Post: dated 29th March
Heading: April, 2008
Post: dated 1st April
Post: dated 4th April

The function takes it’s degree of granularity from the date format you give it. So you can choose to have a month header, and each months posts below each header, or to have a daily header, and all of that days posts below each header. The parameters you pass are exactly the same as for the the_date template tag.

The code

Paste the code below into the functions.php file in your theme, if functions.php doesn’t exist then create it.

// For use on pages which list posts, and to be used within the_loop(). Outputs a
// date header to split up the post listings. See the_date() template tag docs for
// usage: http://codex.wordpress.org/Template_Tags/the_date
function intermittent_date_header( $d='', $before='', $after='', $echo = true )
{
global $idh_last_date_header;
// Get the current date
$date_header = the_date( $d, $before, $after, false );
// If it's the same, don't bother continuing
if ( $date_header == $idh_last_date_header ) return;
// Record the current date header as the last one
$idh_last_date_header = $date_header;
// Decide what to do, and do it. Return, or echo...
if ( ! $echo ) return $date_header;
echo $date_header;
}

Comments and questions welcome. (BTW, if people want this as a plugin, let me know.)

Join the Conversation

16 Comments

  1. Hi Tom – I was worried about putting this snippet up, because it felt like I *must* have missed something and people would laugh and point at the obvious standard function to do it… I’m glad there’s someone to stand next to me when that finally happens. ;)

  2. This is what I’ve been endlessly searching for, but I can’t get it to work. Although I can navigate my way around code, I’m relatively new to all of this. How exactly do I set this up? (I have WordPress 2.6) Any further feedback would be tremendously appreciated.

    Here’s my original functions.php….

    And here’s my index….

    <a href=”” title=””>

    //

    <div class=”post” id=”post-“>

  3. For some reason, my functions code and index code that I pasted into my last comment wouldn’t show up. But any further help in getting a date header to work would be awesome.

  4. Hi Chris. Probably easier to install the plugin (http://wordpress.org/extend/plugins/intermittent-date/) and then add the new template tag to your theme, the template tag needs to be inside The Loop. From the code you sent me, I would put it before the DIV element with a class of “post” (i.e. just at the very beginning of The Loop. As I mention above, the syntax for date display is the same as the standard WordPress date and time template tags.

  5. Hello Simon!

    I’ve been looking for a plugin that did exactly this, but i cannot get it working properly.

    What I need is precisely this, a list of all the posts (in this case by author), but by month:

    August 2008
    Post
    Post
    Post
    July 2008
    Post
    Post
    Post

    I installed the plugin and added the code line, but it’s not working. Here’s the site and the code, i’d really appreciate any help :)))

    http://www.elblogalternativo.com/author/can-men/


    <?php intermittent_date( 'jS F Y', "", "" ); ?> | <a href="" rel="bookmark" title="Permanent Link to ">

  6. Hi Monika. I see no reason why the plugin shouldn’t work in WordPress 2.7, give it a whirl and let us know the results; I’m happy to fix any issues you find.

  7. oh Simon this time I have 45plugins in my *test it Monika* folder…

    and — at Austria a day have only 24 hours – ;)

    ok I copy it in this folder ….

    regards
    Monika (with a blink in my eyes)

  8. You only have 24 hours in Austria?!? Darn it, there goes my “move to Austria for the greater productivity” plan. ;)

  9. oh no! ….
    come! upper austria is so nice..

    wonderful cafes – not only coffee shops – cafes ..there you can sit with one coffee for hours and plan your work … full productivity!
    or our lakes— swimming -trim your body and your soul and you’ll get the best productivity …

    after this you realize there is WP and its plugins and you have to testing them out and you get depressive because it is a hard work to do this so you need one or two hours at a cafe …..

    ..
    .

    ;)

  10. Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  11. Hi! This is just what I need!

    For some reason I seem to be having trouble with the $before and $after params — the codes I put in them simply aren’t showing up (WP 3.3.1). Is anyone else having this behavior, or just me?

    Cheers.

Leave a comment

Leave a Reply to Erik Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.