0

I am working on a calendar script to output events that are grouped by month. Everything is working fine, except I can't figure out how to add a div wrapper around each month without some closing tag issues. Here is my loop code:

if ($posts) {

    $month_titles = array();
    $close = false;  

    foreach( $posts as $post ) {
      setup_postdata( $post ); 
      $month_title = date('F Y', strtotime(get_post_meta($post->ID,'_event_start_local', true)));

      if(!in_array($month_title, $month_titles)) {
        if($close) echo '</ul>';                                                             
        echo '<h4>'.$month_title.'</h4>';
        echo '<ul>';                         
        $month_titles[] = $month_title;                                                         
        $close = true;  
      }
      echo '<li>'.get_the_title().'</li>';

    }
    if ($close) echo '</ul>';
}

Here is how this is current output:

<h4>Month Name 2018</ul>
<ul>
  <li>Title of Event</li>
  <li>Title of Event</li>
</ul>

I would like for it to be like this:

<div>
  <h4>Month Name 2018</h4>
  <ul>
    <li>Title of Event</li>
    <li>Title of Event</li>
  </ul>
</div>

I have tried a few different ways to add the div wrap and it either closes too early or too late. I need a fresh set of eyes on this as I have been messing with it too long!

2 Answers 2

1

This logic should work for what you want:

// preset some vars
$oldMonth = null;
$firstItem = true; 

foreach($posts as $post) {
  setup_postdata($post); 
  $month_title = date('F Y', strtotime(get_post_meta($post->ID,'_event_start_local', true)));

  // check if we have a new month coming up:
  if($oldMonth != $month_title) {
    // when it's the first one ever, just open the div     
    if($firstItem) {
       echo "<div>";
       $firstItem = false;  //..and remember that the following aren't the first
    } else {  // else close the previous ones and open a new one
       echo "</ul></div><div>";
    }
    // show the new month and open the list
    echo '<h4>'.$month_title.'</h4>';
    echo '<ul>';                         
  }
  echo '<li>'.get_the_title().'</li>';
  $oldMonth = $month_title;  // remember the last month

}
// at the end close the last ul and last div
echo '</ul></div>';
Sign up to request clarification or add additional context in comments.

2 Comments

Yep! Thanks! I had to fix a misspelled word on line 1 $oldMoth should be $oldMonth.
yup, corrected that and added some comments! also fixed the end
1

It seems just changing these lines:

if($close) echo '</ul>';                                                             
echo '<h4>'.$month_title.'</h4>';

to

if($close) echo '</ul></div>';                                                             
echo '<div><h4>'.$month_title.'</h4>';

should do what you want. Note you have to change the if on $close in both places in your code)

2 Comments

Jeff beat you by a couple minutes with his solution, but your solution works as well. I upvoted you for your answer. Thanks!
@RiotAct absolutely and normally I wouldn't have answered as Jeff's does work, it just seemed that since your code could be fixed reasonably easily, it was worth posting how to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.