bp_search_pagination( int $total_items, int $items_per_page, int $curr_paged, string $slug, int $links_on_each_side = 2, string $hashlink = '', string $param_key = 'list' )

Prints pagination links for given parameters with pagination value as a querystring parameter.

Description

Instead of printing http://yourdomain.com/../../page/2/, it prints http://yourdomain.com/../../?list=2

If your theme uses twitter bootstrap styles, define a constant : define(‘BOOTSTRAP_ACTIVE’, true) and this function will generate the bootstrap-pagination-compliant html.

Parameters

$total_items

(int) (Required) total number of items(grand total)

$items_per_page

(int) (Required) number of items displayed per page

$curr_paged

(int) (Required) current page number, 1 based index

$slug

(string) (Required) part of url to be appended after the home_url and before the '/?ke1=value1&...' part. withour any starting or trailing '/'

$hashlink

(string) (Optional) the '#' link to be appended ot url, optional

Default value: ''

$links_on_each_side

(int) (Optional) how many links to be displayed on each side of current active page.

Default value: 2

$param_key

(string) (Optional) the name of the queystring paramter for page value. Default 'list'

Default value: 'list'

Return

(void)

Source

File: bp-search/bp-search-functions.php

	function bp_search_pagination( $total_items, $items_per_page, $curr_paged, $slug, $links_on_each_side = 2, $hashlink = "", $param_key = "list" ) {
		$use_bootstrap = false;
		if ( defined( 'BOOTSTRAP_ACTIVE' ) ) {
			$use_bootstrap = true;
		}

		$s = $links_on_each_side; //no of tabs to show for previos/next paged links
		if ( $curr_paged == 0 ) {
			$curr_paged = 1;
		}
		/* $elements : an array of arrays; each child array will have following structure
		  $child[0] = text of the link
		  $child[1] = page no of target page
		  $child[2] = link type :: link|current|nolink
		 */
		$elements    = array();
		$no_of_pages = ceil( $total_items / $items_per_page );
		//prev lik
		if ( $curr_paged > 1 ) {
			$elements[] = array( '←', $curr_paged - 1, 'link' );
		}
		//generating $s(2) links before the current one
		if ( $curr_paged > 1 ) {
			$rev_array = array(); //paged in reverse order
			$i         = $curr_paged - 1;
			$counter   = 0;
			while ( $counter < $s && $i > 0 ) {
				$rev_array[] = $i;
				$i --;
				$counter ++;
			}
			$arr = array_reverse( $rev_array );
			if ( $counter == $s ) {
				$elements[] = array( ' &hellip; ', '', 'nolink' );
			}
			foreach ( $arr as $el ) {
				$elements[] = array( $el, $el, 'link' );
			}
			unset( $rev_array );
			unset( $arr );
			unset( $i );
			unset( $counter );
		}

		//generating $s+1(3) links after the current one (includes current)
		if ( $curr_paged <= $no_of_pages ) {
			$i       = $curr_paged;
			$counter = 0;
			while ( $counter < $s + 1 && $i <= $no_of_pages ) {
				if ( $i == $curr_paged ) {
					$elements[] = array( $i, $i, 'current' );
				} else {
					$elements[] = array( $i, $i, 'link' );
				}
				$counter ++;
				$i ++;
			}
			if ( $counter == $s + 1 ) {
				$elements[] = array( ' &hellip; ', '', 'nolink' );
			}
			unset( $i );
			unset( $counter );
		}
		//next link
		if ( $curr_paged < $no_of_pages ) {
			$elements[] = array( '&rarr;', $curr_paged + 1, 'link' );
		}
		/* enough php, lets echo some html */
		if ( isset( $elements ) && count( $elements ) > 1 ) {
			?>
			<div class="pagination navigation">
				<?php if ( $use_bootstrap ): ?>
				<div class='pagination-links'>
					<?php else: ?>
					<div class="pagination-links">
						<?php endif; ?>
						<?php
						foreach ( $elements as $e ) {
							$link_html = "";
							$class     = "";
							switch ( $e[2] ) {
								case 'link':
									unset( $_GET[ $param_key ] );
									$base_link = get_bloginfo( 'url' ) . "/$slug?";
									foreach ( $_GET as $k => $v ) {
										$base_link .= "$k=$v&";
									}
									$base_link .= "$param_key=$e[1]";
									if ( isset( $hashlink ) && $hashlink != "" ) {
										$base_link .= "#$hashlink";
									}
									$link_html = "<a href='$base_link' title='$e[0]' class='page-numbers' data-pagenumber='$e[1]'>$e[0]</a>";
									break;
								case 'current':
									$class = "active";
									if ( $use_bootstrap ) {
										$link_html = "<span>$e[0] <span class='sr-only'>(current)</span></span>";
									} else {
										$link_html = "<span class='page-numbers current'>$e[0]</span>";
									}
									break;
								default:
									if ( $use_bootstrap ) {
										$link_html = "<span>$e[0]</span>";
									} else {
										$link_html = "<span class='page-numbers'>$e[0]</span>";
									}
									break;
							}

							//$link_html = "<li class='" . esc_attr($class) . "'>" . $link_html . "</li>";
							echo $link_html;
						}
						?>
						<?php if ( $use_bootstrap ): ?>
					</div>
					<?php else: ?>
				</div>
			<?php endif; ?>
			</div>
			<?php
		}
	}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.