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

(Required) total number of items(grand total)

$items_per_page

(Required) number of items displayed per page

$curr_paged

(Required) current page number, 1 based index

$slug

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

$hashlink

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

Default value: ''

$links_on_each_side

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

Default value: 2

$param_key

(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

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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.