Support » Fixing WordPress » Display the date in the loop one time in a month of posts.

  • I’m looking to use something similar to the_date in the loop to display only the month and the year of the post being displayed.

    Unfortunately, the_date will post “September 2012” for every different day of posts in September 2012. I just want it to display once.

    Is that possible?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Yes, it is certainly possible. You just need to save the current date and display the date only when it changes. Here is some sample code:

    if (have_posts()) {
       $current_month = '';
       while (have_posts()) {
          the_post();
          $this_month = get_the_time('F Y');
          if ($current_month != $this_month) {
             $current_month = $this_month;
             echo "<h3>$current_month</h3>";
          }
          // Your Post output here
       }
    }

    Beautiful, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display the date in the loop one time in a month of posts.’ is closed to new replies.