bb_paginate_links

Retrieve paginated links for pages.

Description

(array|string) bb_paginate_links( (string) $args = '' ); 

Technically, the function can be used to create paginated link list for any area. The base argument is used to reference the url, which will be used to create the paginated links. The format. argument is then used for replacing the page number. It is however, most likely and by default, to be used on the archive post pages.

The type argument controls format of the returned value. The default is 'plain', which is just a string with the links separated by a newline character. The other possible values are either array or list. The array value will return an array of the paginated link list to offer full control of display. The list value will place all of the paginated links in an unordered HTML list.

The total argument is the total amount of pages and is an integer. The 'current' argument is the current page number and is also an integer.

An example of the base argument is http://example.com/all_posts.php%_% and the %_% is required. The %_% will be replaced by the contents of in the format. argument. An example for the format. argument is ?page=%#% and the %#% is also required. The %#% will be replaced with the page number.

You can include the previous and next links in the list by setting the 'prev_next' argument to true, which it is by default. You can set the previous text, by using the prev_text argument. You can set the next text by setting the next_text argument.

If the show_all argument is set to true, then it will show all of the pages instead of a short list of the pages near the current page. By default, the show_all is set to false and controlled by the end_size and mid_size arguments. The end_size argument is how many numbers on either the start and the end list edges, by default is 1. The mid_size argument is how many numbers to either side of current page, but not including current page.

It is possible to add query vars to the link by using the add_args argument and see for more information.

Returns (array|string)

String of page links or array of page links.

Parameters (1)

0. $args — Optional. (string) => ''
Override defaults.

Usage

  1. if ( !function_exists( 'bb_paginate_links' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-core.php'; 
  3.  
  4. // Optional. Override defaults. 
  5. $args = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bb_paginate_links($args); 
  9.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-core.php  
  1. function bb_paginate_links( $args = '' ) { 
  2. $defaults = array( 
  3. 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) 
  4. 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number 
  5. 'total' => 1,  
  6. 'current' => 0,  
  7. 'show_all' => false,  
  8. 'prev_next' => true,  
  9. 'prev_text' => __( '« Previous' ),  
  10. 'next_text' => __( 'Next »' ),  
  11. 'end_size' => 1, // How many numbers on either end including the end 
  12. 'mid_size' => 2, // How many numbers to either side of current not including current 
  13. 'type' => 'plain',  
  14. 'add_args' => false, // array of query args to add 
  15. 'add_fragment' => '',  
  16. 'n_title' => __( 'Page %d' ), // Not from WP version 
  17. 'prev_title' => __( 'Previous page' ), // Not from WP version 
  18. 'next_title' => __( 'Next page' ) // Not from WP version 
  19. ); 
  20.  
  21. $args = wp_parse_args( $args, $defaults ); 
  22. extract( $args, EXTR_SKIP ); 
  23.  
  24. // Who knows what else people pass in $args 
  25. $total = (int) $total; 
  26. if ( $total < 2 ) 
  27. return; 
  28. $current = (int) $current; 
  29. $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default. 
  30. $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2; 
  31. $add_args = is_array($add_args) ? $add_args : false; 
  32. $r = ''; 
  33. $page_links = array(); 
  34. $n = 0; 
  35. $dots = false; 
  36.  
  37. $empty_format = ''; 
  38. if ( strpos( $format, '?' ) === 0 ) { 
  39. $empty_format = '?'; 
  40.  
  41. if ( $prev_next && $current && 1 < $current ) { 
  42. $link = str_replace( '%_%', 2 == $current ? $empty_format : $format, $base ); 
  43. $link = str_replace( '%#%', $current - 1, $link ); 
  44. $link = str_replace( '?&', '?', $link ); 
  45. if ( $add_args ) 
  46. $link = add_query_arg( $add_args, $link ); 
  47. $link .= $add_fragment; 
  48. $page_links[] = '<a class="prev page-numbers" href="' . esc_url( $link ) . '" title="' . esc_attr( $prev_title ) . '">' . $prev_text . '</a>'; 
  49.  
  50. for ( $n = 1; $n <= $total; $n++ ) { 
  51. if ( $n == $current ) { 
  52. $n_display = bb_number_format_i18n( $n ); 
  53. $n_display_title = esc_attr( sprintf( $n_title, $n ) ); 
  54. $page_links[] = '<span class="page-numbers current" title="' . $n_display_title . '">' . $n_display . '</span>'; 
  55. $dots = true; 
  56. } else { 
  57. if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) { 
  58. $n_display = bb_number_format_i18n( $n ); 
  59. $n_display_title = esc_attr( sprintf( $n_title, $n ) ); 
  60. $link = str_replace( '%_%', 1 == $n ? $empty_format : $format, $base ); 
  61. $link = str_replace( '%#%', $n, $link ); 
  62. $link = str_replace( '?&', '?', $link ); 
  63. if ( $add_args ) 
  64. $link = add_query_arg( $add_args, $link ); 
  65. $link .= $add_fragment; 
  66. $page_links[] = '<a class="page-numbers" href="' . esc_url( $link ) . '" title="' . $n_display_title . '">' . $n_display . '</a>'; 
  67. $dots = true; 
  68. } elseif ( $dots && !$show_all ) { 
  69. $page_links[] = '<span class="page-numbers dots">…</span>'; 
  70. $dots = false; 
  71. if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) { 
  72. $link = str_replace( '%_%', $format, $base ); 
  73. $link = str_replace( '%#%', $current + 1, $link ); 
  74. if ( $add_args ) 
  75. $link = add_query_arg( $add_args, $link ); 
  76. $link .= $add_fragment; 
  77. $page_links[] = '<a class="next page-numbers" href="' . esc_url( $link ) . '" title="' . esc_attr( $next_title ) . '">' . $next_text . '</a>'; 
  78. switch ( $type ) { 
  79. case 'array': 
  80. return $page_links; 
  81. break; 
  82. case 'list': 
  83. $r .= '<ul class="page-numbers">' . "\n\t" . '<li>'; 
  84. $r .= join( '</li>' . "\n\t" . '<li>', $page_links ); 
  85. $r .= '</li>' . "\n" . '</ul>' . "\n"; 
  86. break; 
  87. default: 
  88. $r = join( "\n", $page_links ); 
  89. break; 
  90. return $r;