Events list short code pagination
WordPress Event Management, Calendars & Registration › Forums › General Question › Events list short code pagination
This topic contains 8 replies, has 2 voices, and was last updated by Stephen Harris 11 years, 5 months ago.
-
AuthorPosts
-
September 1, 2013 at 10:22 pm #7155
Hi,
Using this shortcode can I create pagination? http://wp-event-organiser.com/documentation/shortcodes/event-list-shortcode/
Or can I somehow duplicate the events archive page and only show past events.
The reason I have to do this is because if I show past events, the events page will start at the first ever event and not the next event closest todays date. Maybe there is some way to fix this? π
Daniel PetersSeptember 2, 2013 at 5:45 pm #7159Hi Daniel,
You can toggle whether or not to show pat events on the archive page via the plug-in settings (General tab), is that what you’re after? (Or maybe I’ve misunderstood π )
Stephen HarrisSeptember 2, 2013 at 5:58 pm #7161Hi,
If I turn on show past events it starts the events list at the very first event, I need it to start at the event closest to todays date, is this possible?
Daniel PetersSeptember 2, 2013 at 6:50 pm #7162Ok, so I didn’t read this properly:
Or can I somehow duplicate the events archive page and only show past events.
Just to be sure then, you want a paginated events page showing only past events? And you wanted this is reversed,
so that that most recently ‘past’ event is first?Then should do mean that
events/event?myprefix=past-events
displays the archive page but with only past events.
Theevents/event
base is will need to change depending on how you’ve configured the plug-in.add_action( 'pre_get_posts', 'myprefix_past_events_page', 5 ); function myprefix_past_events_page( $query ){ if( eventorganiser_is_event_query( $query ) ){ if( $query->get( 'myprefix' ) && 'past-events' == $query->get( 'myprefix' ) ){ //Show only past events. $query->set( 'showpastevents', true ); //This is needed for now, but maybe redundant in the future... $query->set( 'event_start_before', 'now' ); //Reverse the order: $query->set( 'order', 'DESC' ); $query->set( 'orderby', 'eventstart' ); } } } add_filter('query_vars', 'myprefix_register_query_vars' ); function myprefix_register_query_vars( $qvars ){ $qvars[] = 'myprefix'; return $qvars; }
This bit is entirely optional, but makes it pretty, i.e. the page can be accessed by
events/event/past-events
.add_action( 'init', 'myprefix_add_rewrite_rule', 11 ); function myprefix_add_rewrite_rule(){ global $wp_rewrite; //Get base regex $regex = str_replace( '%event%', 'past-events', $wp_rewrite->get_extra_permastruct('event') ); //Get pagination base regex $pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$'; //Add paged rewrite rule add_rewrite_rule( $regex.'/'.$pageregex, 'index.php?post_type=event&paged=$matches[1]&event_start_before=now&myprefix=past-events', 'top' ); //Add standard rewrite urle add_rewrite_rule( $regex, 'index.php?post_type=event&event_start_before=now&myprefix=past-events', 'top' ); }
Stephen HarrisSeptember 2, 2013 at 6:59 pm #7163Hey thanks for this but I don’t think I was clear enough sorry.
Currently I am using the events page without showing past events, so it starts at the next event: http://78.129.175.88/~sapphire/events/event/
Which is fine, but then I want to show past events so I added a new page and used the events list shortcode to show past events: http://78.129.175.88/~sapphire/events/past-events/
So my question was can I paginate the events list short code so that my past events page is paginated, which I think the code you posted above would do?
However, I would much rather show past events on the page ‘http://78.129.175.88/~sapphire/events/event/’ which I know I can do in the events settings, but when I set this to show past events then that page will start listing events at the first ever created event, so then my question is can I change this to start listing at the next event like it currently does, but allow you to go back like the ‘β Newer events’ link does if you go to later events?
Daniel PetersSeptember 2, 2013 at 7:42 pm #7164Hi Daniel,
It’s not really possible to paginate the event list shortcode due to how WordPress works.
my question is can I change this to start listing at the next event like it currently does, but allow you to go back like the ββ Newer eventsβ link does if you go to later events?
… that’s an interesting question. Unfortunately that’s really quite difficult to implement, since WordPress’ pagination handling was never intended for things like that. The closest you can could easily get to is to map
/events/event
to map toevents/event/page/X
whereX
is the page on which the next occurring event livesBelow is a snippet that should do that, but it comes with a health warning. You have to query the events a second time to work out the page number. There are also various assumptions made that may mean it requires tweaking before it will work without issue, but it works on my test install.
Obviously you should have ‘show past events’ set to ‘yes’.
add_action( 'pre_get_posts', 'myprefix_jump_to_current', 5 ); function myprefix_jump_to_current( $query ){ global $wp_query, $wp_rewrite; $paged = (int) $query->get('paged'); if( eventorganiser_is_event_query( $query ) && $query->is_main_query() && ( $paged == 0 ) ){ //Count how many events occur before current one: $events = eo_get_events( array( 'fields'=>'ids', 'event_start_before' => 'now', 'posts_per_page' => -1, ) ); //Translate that to total number of pages $per_page = absint( get_option('posts_per_page') ); $pages = floor( ( count( $events ) + 1 ) / $per_page ); //Set the paged value accordingly.... if( $pages != 0 && $pages != 1 ){ $query->set('paged', $pages ); } } }
Hope that helps!
Stephen HarrisSeptember 2, 2013 at 8:16 pm #7165Thank you I will try this tonight. π
Daniel PetersSeptember 3, 2013 at 8:26 am #7166Hi sorry, where do I put this file? i’ve tried it in the archive-event template and my theme functions file?
Daniel PetersSeptember 3, 2013 at 9:23 am #7167Ideally it should go in a ‘utility plug-in’, but it’ll work just as well as in your theme’s
functions.php
.You can use
wp_die( var_dump( [some variable] ) )
, e.g.wp_die(var_dump( $events) )
in the code to kill the page and inspect the passed variable to help debug.Stephen Harris -
AuthorPosts