Tribe__Events__Community__Main::pagination( object $query, int $pages, int $range = 3, bool $shortcode = false )

Filter pagination


Parameters

$query

(object) (Required) The query to paginate

$pages

(int) (Required) The pages

$range

(int) (Optional) The range

Default value: 3

$shortcode

(bool) (Optional)

Default value: false


Top ↑

Return

(string) The pagination links


Top ↑

Source

File: src/Tribe/Main.php

		public function pagination( $query, $pages = 0, $range = 3, $shortcode = false ) {
			$output    = '';
			$showitems = ( $range * 2 ) + 1;

			global $paged;
			if ( empty( $paged ) ) {
				$paged = 1;
			}

			if ( $pages == 0 ) {
				//global $wp_query;
				$pages = ceil( $query->found_posts / $this->eventsPerPage );

				if ( ! $pages ) {
					$pages = 1;
				}
			}

			if ( $paged > $pages ) {
				$this->enqueueOutputMessage( __( 'The requested page number was not found.', 'tribe-events-community' ) );
			}
			if ( 1 != $pages ) {
				add_filter( 'get_pagenum_link', array( $this, 'fix_pagenum_link' ) );

				// If we are using the Community Events Shortcode, we should paginate the current post URL
				if ( $shortcode ) {
					// Ensure that the URLs will always end with slash.
					// This is necessary for the Events List to be paginated on posts or pages with ugly permalinks.
					$url = rtrim( get_permalink(), '/' ) . '/';

					$output .= "<div class='tribe-pagination'>";
					if ( $paged > 2 && $paged > $range + 1 && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( $url . $paged ) . "'>&laquo;</a>";
					}
					if ( $paged > 1 && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( $url . ( $paged - 1 ) ) . "'>&lsaquo;</a>";
					}

					for ( $i = 1; $i <= $pages; $i ++ ) {
						if ( 1 != $pages && ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) ) {
							$output .= ( $paged == $i ) ? '<span class="current">' . $i . '</span>' : '<a href="' . esc_url( $url . $i ) . '" class="inactive">' . $i . '</a>';
						}
					}

					if ( $paged < $pages && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( $url . ( $paged + 1 ) ) . "'>&rsaquo;</a>";
					}
					if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( $url . $paged ) . "'>&raquo;</a>";
					}
					$output .= "</div>\n";
				} else {
					$output .= "<div class='tribe-pagination'>";
					if ( $paged > 2 && $paged > $range + 1 && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( get_pagenum_link( 1 ) ) . "'>&laquo;</a>";
					}
					if ( $paged > 1 && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( get_pagenum_link( $paged - 1 ) ) . "'>&lsaquo;</a>";
					}

					for ( $i = 1; $i <= $pages; $i ++ ) {
						if ( 1 != $pages && ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $showitems ) ) {
							$output .= ( $paged == $i ) ? '<span class="current">' . esc_html( $i ) . '</span>' : '<a href="' . esc_url( get_pagenum_link( $i ) ) . '" class="inactive">' . esc_html( $i ) . '</a>';
						}
					}

					if ( $paged < $pages && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( get_pagenum_link( $paged + 1 ) ) . "'>&rsaquo;</a>";
					}
					if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ) {
						$output .= "<a href='" . esc_url( get_pagenum_link( $pages ) ) . "'>&raquo;</a>";
					}
					$output .= "</div>\n";
				}
			}

			return $output;
		}

Top ↑

Changelog

Changelog
Version Description
1.0 Introduced.