Determining the last date for an event

WordPress Event Management, Calendars & Registration Forums General Question Determining the last date for an event

This topic contains 11 replies, has 2 voices, and was last updated by  Adam Abrams 9 years, 7 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #18304

    Hi! I’d like my listing of events to include either the date (for a single-date event) or a comma-delimited list of dates (for a multi-day event).

    To do this, I’ve borrowed the code from event-meta-event-single.php that shows the upcoming occurrences of a multi-day event, and put a modified form of it into shortcode-event-list.php (actually, a copy of that file, located in my theme folder).

    I need to determine, within the query loop, if I’m on the last instance of an event, so I can omit the comma which I otherwise want to have after each date. But, despite trying a variety of promising approaches, I can’t get this to work. Code like:

    <?php if (($wp_query->current_post +1) == ($wp_query->post_count)) {
    echo 'This is the last post';
    } ?>
    

    …doesn’t seem to do the trick.

    Here’s the entirety of what I’ve added (minus any attempt to test for the “last post”):

    <?php if( eo_reoccurs() ){          
                    //Event reoccurs - display dates. 
                    $upcoming = new WP_Query(array(
                        'post_type'=>'event',
                        'event_start_after' => 'today',
                        'posts_per_page' => -1,
                        'event_series' => get_the_ID(),
                        'group_events_by'=>'occurrence'//Don't group by series
                    ));
    
                    if( $upcoming->have_posts() ): ?>
    
                        <?php _e('Upcoming Dates','eventorganiser'); ?>:
                            

    <?php while( $upcoming->have_posts() ): $upcoming->the_post(); ?> <?php eo_the_start($date_format) ?> <?php endwhile; ?>

    <?php wp_reset_postdata(); //With the ID 'eo-upcoming-dates', JS will hide all but the next 5 dates, with options to show more. wp_enqueue_script('eo_front'); ?> <?php endif; ?> <?php } ?>

    Can you advise on what kind of test I should be running in the loop to achieve my goal?

    Many thanks! Adam

    Adam Abrams
    #18305

    I also realize that my code also causes the excerpt for a multi-date event to not display at all…. so clearly I’m way off base on my approach! 8^(

    Adam Abrams
    #18306

    Hi Adam,

    An easier way to do this is to create an array ($array) whose values are the formatted start dates of the event’s occurrences (using eo_get_the_start() rather than eo_the_start()). Then you can just call echo implode( ', ', $array );

    Stephen Harris
    #18322

    Thanks Stephen! I added the following:

    <!-- List date or dates of film  -->
    
    <?php 
    $movieDates = array (eo_get_the_start());
    echo implode( ', ', $movieDates );
    ?>
    

    But I’m only seeing the first of the two dates for my example film listing. (Both dates show in the usual manner on the event detail page.)

    Screen Shot

    I studied up on the array and implode functions but clearly I’ve missed something here….?

    Adam Abrams
    #18323

    You’re creating an array with only one date in it. You need to loop over the occurrences and add each to the array:

    <?php
    $movieDates  = array();
    while( $upcoming->have_posts() ): $upcoming->the_post();
        $movieDates[] = eo_get_the_start( $date_format );
    endwhile;
    echo implode( ', ', $movieDates );
    wp_reset_postdata(); 
    ?>
    
    Stephen Harris
    #18325

    Thanks Stephen. I swapped in your code but I now get an error “Undefined variable: upcoming”. Clearly still missing something…

    Perhaps I should just show my entire shortcode-event-list.php code. Here it is:

    http://pastebin.com/pZsR4Mm5

    You can see where I’ve placed your code about 2/3 down.

    Does $upcoming need to be defined at the top in the “list ID / classes” section?

    • Adam
    Adam Abrams
    #18326

    $upcoming is assumed to be a WP_Query instance

    Since you’re already within a loop, it might be easier to use the following:

       $occurrences = eo_get_the_occurrences_of( get_the_ID() );
       //or $occurrences = eo_get_the_future_occurrences_of( get_the_ID() );
       $movieDates = array();
       foreach( $occurrences as $occurrence_id => $occurrence ){
            $movieDates[] = $occurrence['start']->format('jS F Y');
       }
       echo implode( ', ', $movieDates );
    
    • This reply was modified 9 years, 7 months ago by  Stephen Harris.
    • This reply was modified 9 years, 7 months ago by  Stephen Harris.
    Stephen Harris
    #18327

    Thanks! However, now I get this error:

    Parse error: syntax error, unexpected T_FOREACH in /home2/vjffs122/public_html/wordpress/wp-content/themes/genesis-sample/shortcode-event-list.php on line 74

    …referring to the line that starts “foreach( etc.” .

    This occurs whether I use the first or second version of the line defining $occurrences.

    Adam Abrams
    #18329

    There was a missing semicolon after $movieDates = array() (now fixed)

    Stephen Harris
    #18346

    Thanks again Stephen! But now that code simply doesn’t display anything. I’ve read up on array assignment, the eo_get_the_occurrences_of function, etc., to try and get to the bottom of it myself but I”m afraid I’m still stumped – it certainly seems like it should work!

    Adam Abrams
    #18349

    Another typo: It should have been $movieDates not $moveDates inside the foreach. I’ve now corrected it.

    Stephen Harris
    #18351

    Darn typos! I should have checked for that… but that’s why typos are so insidious, they always sneak up you…

    At any rate… IT WORKS! Hooray!

    Thanks SO much for your help, Stephen. All the best!

    Adam Abrams
Viewing 12 posts - 1 through 12 (of 12 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.