Support » Fixing WordPress » Using monthly marker instead of daily date for posts in loop?

  • Resolved loulantos

    (@loulantos)


    Hi,

    Rather than using the standard date format (d/m/y in the UK) I wanted to just use the month and the year, once, as a marker.

    I’m finding a lot of posts about doing this in an archive post, but I’d like to do this in my main loop, so that it looks like…

    JULY 2013
    Post 567
    Post 566
    Post 565

    JUNE 2013
    Post 564
    Post 563

    So on and so forth… Is this possible?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with a loop like this:

    <?php
    $temp_year = $temp_month = '';
    ?>
    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php
    $Year= get_the_date( 'Y' );
    $Month = get_the_date( 'F' );
    if ( $Month != $temp_month || $Year != $temp_year ) {
    	echo '<h2>' . $Month . ' ' . $Year . '</h2>';
    }
    ?>
    
    <!-- your loop code -->
    
    <?php
    // at bottom of the loop
    $temp_year = $Year;
    $temp_month = $Month;
    ?>
    <?php endwhile; ?>
    <?php endif; ?>

    Thread Starter loulantos

    (@loulantos)

    Thanks, that’s pretty much exactly what I was after. Appreciate it!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using monthly marker instead of daily date for posts in loop?’ is closed to new replies.