WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] The Loop per month (3 posts)

  1. harrrm
    Member
    Posted 5 years ago #

    I've customized a template to my own needs.
    I want to show all my posts on the index page and want to show a divider per month.
    So at the beginning of a new month there is a new heading showing the months number and year.

    Somethingling like this:

    <h2>02/09</h2>
    <ul>
      <li>post 1</li>
    </ul>
    <h2>01/09</h2>
    <ul>
      <li>post 2</li>
      <li>post 3</li>
    </ul>

    Does anyone know how to fix this in the loop?

  2. Konstantin Kovshenin
    Member
    Posted 5 years ago #

    Nice question. A workaround might be having a variable store the current month, and if it changed inside the loop, output the h2. Something like this:

    $last_month = 0;
    
    // the loop
    ...
    
    $month = get_the_time('n');
    $year = get_the_time('Y');
    
    if ($month != $last_month)
    {
      echo "</ul><h2>$month/$year</h2><ul>";
      $last_month = $month;
    }
    
    ...
    // end of loop ..
  3. harrrm
    Member
    Posted 5 years ago #

    Superb!

    Works like a train!

Topic Closed

This topic has been closed to new replies.

About this Topic