Insert element midway through wordpress loop according to post condition

Sorry for a probably dim question but I can’t find the correct terminology to find the solution.

I’ve got a wordpress loop [a php while loop] for my post_type event. Each post has a value with a date string. I am comparing this string with the current date. The date values start in the future and end a few years ago.

Read More

When the loop gets to posts with date values lower than the current date [$date > $eventdate] I want to insert some code [a divider <article>] and then continue my loop. How do I do this?

Seems very simple but please help as I’m stuck!

EDIT: I tried to nest two conditional while loops with the divider code in between but that was very bad. Then tried something similar with if’s but I got that wrong too. Here’s the [simplified] code to work with:

    <?php
    $date = date(Ymj);
      if ( have_posts() ) : while ( have_posts() ) : the_post();
        $eventdate = get_post_meta($post->ID, 'event-date', true); ?>
    <article>post goes here</article>
      <?php endwhile; else: 
    ?>

Related posts

Leave a Reply

2 comments

  1. <?php
    $did_article = false;
    $now = time();
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        $eventdate = get_post_meta( $post->ID, 'event-date', true );
        $eventdate = strtotime( $eventdate );
        if ( !$did_article ) {
            if ( $now > $eventdate ) {
                $did_article = true;
                // Do special article stuff here
            }
        } ?>
        Do regular post suff here
    <?php endwhile; endif; ?>
    
  2.  <?php
    $date = date(Ymj);
    $datetime = strtotime($date);
    
      if ( have_posts() ) : while ( have_posts() ) : the_post();
        $eventdate = get_post_meta($post->ID, 'event-date', true); ?>
        $eventdatetime = strtotime($eventdate); //$ventdatetime must be seconds
        if ($datetime > $eventdatetime) {
              <!--here goes the code for past events-->
        }
        <article>post goes here</article>
      <?php endwhile; else: 
    ?>