WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display the date in the loop one time in a month of posts. (3 posts)

  1. Andrew Jacob Johnson
    Member
    Posted 1 year ago #

    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?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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
       }
    }
  3. Andrew Jacob Johnson
    Member
    Posted 1 year ago #

    Beautiful, thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags