I'm working on a page which displays a list of radioshows sorted by date and time. The radioshow is a custom posttype with a name, pic, description and date/time info. There is a variable number of radioshows per day.
The list is displayed and sorted correctly, but I would like to display a weekday at the start of a new day so the list becomes more readable.
Is there a way to test the date of the current item in the loop against the previous item? So I can test if the date has changed and then echo out the weekday once?
Additional information: I looked into the the_date() function to acomplish this. However, this function only returns the date the custom post itself was created, not the date field when the show is on. That date is a custom field and differs from the date of the post.
Edit: This is the working code, based on the example by @s_ha_dum that makes the weekday only display once in the loop, as long as it is on the same date.
<?php $args = array(
'post_type' => 'programme',
'posts_per_page' => 12,
'orderby' => 'prog_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'time',
'value' => '',
'compare' => 'LIKE'
)
)
);`
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) {
$post_date = '';
// setlocale(LC_TIME, 'nl_NL');
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php $date = DateTime::createFromFormat('Ymd', get_field('date')); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('index-card row'); ?>>
<?php
if ($post_date != $date) {
?>
<div class="weekday"><h3><?php echo $date->format('l'); ?></h3></div>
<?php
$post_date = $date;
}
?>
<header class="medium-4 columns text-center">
<div class="prog-time"><p><i class="fa fa-calendar"></i> <?php echo $date->format('d/m/Y'); ?> om <?php the_field( 'time' ); ?> uur</p></div>
<img src="<?php the_field( 'afbeelding' ); ?>" alt="" class="prog-img">
<p>Presentatie:<br /><?php the_field( 'presenter' ); ?></p>
</header>
<div class="entry-content medium-8 columns">
<h4 class="prog-title"><?php the_title(); ?></h4>
<h4 class="prog-sub subheader"><?php the_field( 'name' ); ?></h4>
<div class="prog-desc"><?php the_field( 'description' ); ?></div>
</div>
<hr>
</article>
<?php
endwhile;
}
?>