0

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; 
            } 
         ?> 
  • Please accept the answer from @s_ha_dum as it solved your issue. Just click on the check mark next to his answer, it will go green :-) – Pieter Goosen Jun 8 '15 at 14:23
0

You need something like this:

$args = array(
  'post_type' => 'post'
);
$q = new WP_Query($args);

if ($q->have_posts()) {
  $date = '';
  while ($q->have_posts()) {
    $q->the_post();
    $m = get_post_meta($post->ID,'field_id',true);
//     var_dump($m);

    if ($date != $m) {
      // if $m is a strtotime compatible string
      echo date('l',strtotime($m)); 
      $date = $m;
    }

    echo '<br>';
    the_title();
    echo '<br>';
    echo str_repeat('-',200);
    echo '<br>';
  }
}

A lot depends on the format of your "date/time info" in the custom meta field though, and you do not specify. I am also not sure if that meta_key is unique per post. I assumed it was.

  • Thanks for helping out. The date format is Ymd. This was what I was looking for. Now there is a variable with the date outside the while loop to test against. I'll give it a try today thanks! – SdeWijs Jun 8 '15 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.