Support

Account

Home Forums General Issues Helping event posts displaying by month

Solved

Helping event posts displaying by month

  • Hello all, I try to view events sorted by months, I found this forum a code that works for display in the current dates,

    <?php
    	// filter
    $today = date('Ymd');
    $args = array (
    'post_type'		=> 'events',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'post_status' => 'publish',
    'meta_key' => 'date_start',
    'meta_query' => array(
    'relation' => 'OR',
    array(
    	'key' => 'date_start',
    	'value' => date('Ymd', strtotime('now')),
    	'type' => 'numeric',
    	'compare' => '>=',
    	),
    array(
    	'key' => 'date_end',
    	'value' => date('Ymd', strtotime('now')),
    	'type' => 'numeric',
    	'compare' => '>=',
    	),
    ),
    );
    	$the_query = new WP_Query($args) ; ?>
    	<?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li class="list-representations">
    //month display tag
    	<div class="list-separator-month">
    		<h2>
    		<?php $dateformatstring = "F Y"; $unixtimestamp =				strtotime(get_field('date_start')); echo date_i18n($dateformatstring, $unixtimestamp); ?>
    		</h2>
    	</div>
    	<article>
    		<h1>
    		<?php the_title(); ?> :
       		 <span>du<strong><?php $dateformatstring = "l d F"; $unixtimestamp = strtotime(get_field('date_start')); echo date_i18n($dateformatstring, $unixtimestamp); ?></strong>
    			au <strong>
    <?php $dateformatstring = "l d F"; $unixtimestamp = strtotime(get_field('date_end')); echo date_i18n($dateformatstring, $unixtimestamp); ?></strong></span>
    </h1>
    	<div class="text_resume-list">
    		<?php the_field('text_resume'); ?>
    	</div>
    </li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    <?php wp_reset_query();	?>
    </article>

    However, it appears to me something like this:

    March 2016
       my events: March 21, 2016

    April 2016
      my events: April 1, 2016

    April 2016
      my events: April 5, 2016

    What I want to see:

    March 2016
       my events: March 21, 2016

    April 2016
      my events: April 1, 2016
      my events: April 5, 2016

    I have the impression of not being far …
    Thank you to bring me a little light in the tunnel (wp_query)

  • Hi @ikazi

    You need to save the month in a temporary variable and then check if the current month is the same with the one in the temporary variable or not. Maybe something like this:

    <?php if( $the_query->have_posts() ): ?>
    <?php $current_month = ""; ?>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li class="list-representations">
    //month display tag
    	<div class="list-separator-month">
    		<h2>
    		<?php
    			$dateformatstring = "F Y";
    			$unixtimestamp = strtotime(get_field('date_start'));
    			$pretty_month = date_i18n($dateformatstring, $unixtimestamp);
    			if ($current_month != $pretty_month){
    				echo $pretty_month;
    				$current_month = $pretty_month;
    			}
    			?>
    		</h2>
    	</div>
    	<article>

    Hope this helps.

  • Thanks!!
    This is exactly what I was looking for.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Helping event posts displaying by month’ is closed to new replies.