Support

Account

Home Forums Front-end Issues Month divider between events

Helping

Month divider between events

  • Hi
    I’d like to insert a divider that separates out my CPT ‘programme’ with a divider declaring the month. I’ve seen a few techniques to do this but need a way to format like so…

    <?php 
    $posts = get_posts(array(
      'post_type' => 'programme',
      'meta_key'  => 'event_start',
      'orderby'   => 'meta_value_num',
      'order'     => 'ASC',
    ));
    if( $posts ): ?>
    <?php foreach( $posts as $post ): 
    	  setup_postdata( $post )
    	  ?>

    so that I can do all the usual stuff between… i.e.

    <div class="event">
    		
    			<?php
    			$start_date = get_field('event_start');
    			$end_date = get_field('event_end');
    			echo $start_date;
    			if ($end_date != $start_date) {
    			  echo ' — ',$end_date;
    			}
    			?>
    			
    </div>

    Many of the examples I’ve seen require me to echo out every field inside the query which is not ideal in this instance.
    Any tips would be greatly appreciated

  • 
    $last_month = '';
    foreach ($posts as $post) {
      setup_postdata($post);
      $start_date = get_field('event_start');
      $month = date('F', strtotime($start_date));
      if ($month != $last_month) {
        echo '<h2>'.$month.'</h2>';
      }
      $last_month = $month;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.