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.
-
AuthorPosts
-
August 5, 2015 at 10:21 pm #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
August 5, 2015 at 10:34 pm #18305I 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
August 6, 2015 at 1:04 am #18306Hi 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 (usingeo_get_the_start()
rather thaneo_the_start()
). Then you can just callecho implode( ', ', $array );
Stephen Harris
August 6, 2015 at 10:41 pm #18322Thanks 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.)
I studied up on the array and implode functions but clearly I’ve missed something here….?
Adam Abrams
August 6, 2015 at 11:22 pm #18323You’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
August 7, 2015 at 12:17 am #18325Thanks 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:
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
August 7, 2015 at 12:27 am #18326$upcoming
is assumed to be aWP_Query
instanceSince 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
August 7, 2015 at 12:38 am #18327Thanks! 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
August 7, 2015 at 12:43 am #18329There was a missing semicolon after
$movieDates = array()
(now fixed)Stephen Harris
August 7, 2015 at 4:49 pm #18346Thanks 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
August 7, 2015 at 7:29 pm #18349Another typo: It should have been
$movieDates
not$moveDates
inside theforeach
. I’ve now corrected it.Stephen Harris
August 7, 2015 at 8:53 pm #18351Darn 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
-
AuthorPosts