Skip to content

Instantly share code, notes, and snippets.

@barryhughes

barryhughes/nav.php

Created Nov 26, 2014
Embed
What would you like to do?
Template override for list/nav.php (TEC 3.8.x) - tests possible efficiency savings in list view pagination
<?php
/**
* List view nav template (making use of alternative pagination functions)
*
* This template isn't really the proper place for the two template definitions included
* at the top, but they could easily be moved to a more suitable location. They've been
* included in this file for no reason other than ease of demonstrating and sharing a
* workaround.
*/
defined( 'ABSPATH' ) or exit( '-1' );
if ( ! function_exists( 'tribe_are_there_previous_events' ) ) {
/**
* Try to determine if there are previous events we need to link to.
*
* @return bool
*/
function tribe_are_there_previous_events() {
global $wp_query;
$upcoming = tribe_is_upcoming();
$past = ! $upcoming;
$cur_page = (int) $wp_query->get( 'paged' );
$max_pages = (int) $wp_query->max_num_pages;
$page_1 = 0 === $cur_page || 1 === $cur_page;
// Simple tests based on pagination properties
if ( $upcoming && $cur_page > 1 ) return true;
if ( $past && $cur_page < $max_pages ) return true;
// Test for past events (on first page of upcoming list only)
if ( $upcoming && $page_1 ) {
$args = (array) $wp_query->query;
$args['eventDisplay'] = 'past';
$args['no_paging'] = true;
$args['no_found_rows'] = true;
$args['posts_per_page'] = 1;
$past_events = tribe_get_events( $args );
return ( count( $past_events ) >= 1 );
}
// Undetermined
return false;
}
}
if ( ! function_exists( 'tribe_are_there_more_events' ) ) {
/**
* Try to determine if there are more events to page forward into.
*
* @return bool
*/
function tribe_are_there_more_events() {
global $wp_query;
$upcoming = tribe_is_upcoming();
$past = ! $upcoming;
$cur_page = (int) $wp_query->get( 'paged' );
$max_pages = (int) $wp_query->max_num_pages;
$page_1 = 0 === $cur_page || 1 === $cur_page;
// Simple tests based on pagination properties
if ( $upcoming && $cur_page < $max_pages ) return true;
if ( $past && $cur_page > 1 ) return true;
// Test for future events (on first page of the past events list only)
if ( $past && $page_1 ) {
$args = (array) $wp_query->query;
$args['eventDisplay'] = 'list';
$args['no_paging'] = true;
$args['no_found_rows'] = true;
$args['posts_per_page'] = 1;
$upcoming_events = tribe_get_events( $args );
return ( count( $upcoming_events ) >= 1 );
}
// Undetermined
return false;
}
}
?>
<h3 class="tribe-events-visuallyhidden"><?php _e( 'Events List Navigation', 'tribe-events-calendar' ) ?></h3>
<ul class="tribe-events-sub-nav">
<!-- Left Navigation -->
<?php if ( tribe_are_there_previous_events() ) : ?>
<li class="<?php echo tribe_left_navigation_classes(); ?>">
<a href="<?php echo tribe_get_listview_link() ?>" rel="prev"><?php _e( '<span>&laquo;</span> Previous Events', 'tribe-events-calendar' ) ?></a>
</li><!-- .tribe-events-nav-left -->
<?php endif; ?>
<!-- Right Navigation -->
<?php if ( function_exists( 'tribe_are_there_more_events' ) && tribe_are_there_more_events() ) : ?>
<li class="<?php echo tribe_right_navigation_classes(); ?>">
<a href="<?php echo tribe_get_listview_link() ?>" rel="next"><?php _e( 'Next Events <span>&raquo;</span>', 'tribe-events-calendar' ) ?></a>
</li><!-- .tribe-events-nav-right -->
<?php endif; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.