Dear Stephen,
I have tried to implement Next Event Link and Prev Event Link on the page Single Event,
but still I can not find the right solution for that.
The original WP buttons for the navigation are made for “sorting” by date of publishing
the post / event.
But I would like to “sort” events by “event_start_before” and “event_end_before”, etc. of course only for events.
Can you help me with this, please?
Thank you, Radim.
Radim Socha
Hi Radim,
This isn’t generally possible. The single event page is the page for all occurrences of an event. So there is no well defined ‘next’ and ‘previous’ event for recurring event (before or after which occurrence?).
For non-recurring events, there is no ambiguity and you can use the snippet given in this thread: https://wordpress.org/support/topic/how-do-you-add-previousnext-buttons-to-a-single-event. Copied below for completeness:
$next_event = eo_get_events( array(
'post_not__in' => array( get_the_ID() ),
'event_start_after' => eo_get_the_start( 'Y-m-d H:i:s' )
) );
if( $next_event ){
$permalink = get_permalink( $next_event[0] );
printf( '<a href="%s">Next event</a>', $permalink );
}
and similarly an (incomplete) example for displaying a link to the previous event:
$previous_event = eo_get_events( array(
'post_not__in' => array( get_the_ID() ),
'event_start_before' => eo_get_the_start( 'Y-m-d H:i:s' ),
'showpastevents' => true,
) );
Stephen Harris
Stephen, thank you for so quickly feedback… I have little rebuild the code, because it seems does not work the directive 'post_not__in' => array( get_the_ID() )
for me. Well, my final code is:
<?php
$next_permalink = "";
$next_permalinkTitle = "";
$next_event = eo_get_events( array(
'post_not__in' => array( get_the_ID() ),
'event_start_after' => eo_get_the_start( 'Y-m-d H:i:s' ),
'numberposts'=>2
)
);
if( $next_event ) {
if( $next_event[0]->ID != get_the_ID() ){
// absolutely first event
$next_permalink = get_permalink( $next_event[0] );
$next_permalinkTitle = get_the_title( $next_event[0] );
} else
if($next_event[1]) {
$next_permalink = get_permalink( $next_event[1] );
$next_permalinkTitle = get_the_title( $next_event[1] );
}
//printf( '<a href="%s">Next event</a>', $next_permalink );
}
$prev_permalink = "";
$prev_permalinkTitle = "";
$prev_event = eo_get_events( array(
'post_not__in' => array( get_the_ID() ),
'event_start_before' => eo_get_the_start( 'Y-m-d H:i:s' ),
'order' => 'DESC',
'showpastevents' => true,
'numberposts'=>2
)
);
if( $prev_event ) {
if ($prev_event[0]->ID != get_the_ID()) {
$prev_permalink = get_permalink( $prev_event[0] );
$prev_permalinkTitle = get_the_title( $prev_event[0] );
} else
if ($prev_event[1]) {
// absolutely last event in future
$prev_permalink = get_permalink( $prev_event[1] );
$prev_permalinkTitle = get_the_title( $prev_event[1] );
}
//printf( '<a href="%s">Previous event</a>', $prev_permalink );
}
echo($next_permalink)?'<div class="alignleft">'.(__('«Next Event ', TEMPLATE_DOMAIN)).'<a href="'.$next_permalink.'">'.$next_permalinkTitle.'</a>':"";
echo($prev_permalink)?'<div class="alignright">'.(__(' Previous Event»', TEMPLATE_DOMAIN)).'<a href="'.$prev_permalink.'">'.$prev_permalinkTitle.'</a></div>':"";
?>
Maybe it could help to others.
Radim
Radim Socha
Thanks for posting this Radim. You may want to look at what get_the_ID()
is returning, it should be the ID of the current event.
Stephen Harris