get_pagenum_link

Retrieves the link for a page number.

Description

(string) get_pagenum_link( (int) $pagenum = 1, (bool) $escape = true ); 

Returns (string)

The link URL for the given page number.

Parameters (2)

0. $pagenum — Optional. (int) => 1
Page ID. Default 1.
1. $escape — Optional. (bool) => true
Whether to escape the URL for display, with esc_url(). Defaults to true. Otherwise, prepares the URL with esc_url_raw().

Usage

  1. if ( !function_exists( 'get_pagenum_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Page ID. Default 1. 
  5. $pagenum = 1; 
  6.  
  7. // Optional. Whether to escape the URL for display, with esc_url(). Defaults to true. 
  8. // Otherwise, prepares the URL with esc_url_raw(). 
  9. $escape = true; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = get_pagenum_link($pagenum, $escape); 
  13.  

Defined (1)

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

/wp-includes/link-template.php  
  1. function get_pagenum_link($pagenum = 1, $escape = true ) { 
  2. global $wp_rewrite; 
  3.  
  4. $pagenum = (int) $pagenum; 
  5.  
  6. $request = remove_query_arg( 'paged' ); 
  7.  
  8. $home_root = parse_url(home_url()); 
  9. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : ''; 
  10. $home_root = preg_quote( $home_root, '|' ); 
  11.  
  12. $request = preg_replace('|^'. $home_root . '|i', '', $request); 
  13. $request = preg_replace('|^/+|', '', $request); 
  14.  
  15. if ( !$wp_rewrite->using_permalinks() || is_admin() ) { 
  16. $base = trailingslashit( get_bloginfo( 'url' ) ); 
  17.  
  18. if ( $pagenum > 1 ) { 
  19. $result = add_query_arg( 'paged', $pagenum, $base . $request ); 
  20. } else { 
  21. $result = $base . $request; 
  22. } else { 
  23. $qs_regex = '|\?.*?$|'; 
  24. preg_match( $qs_regex, $request, $qs_match ); 
  25.  
  26. if ( !empty( $qs_match[0] ) ) { 
  27. $query_string = $qs_match[0]; 
  28. $request = preg_replace( $qs_regex, '', $request ); 
  29. } else { 
  30. $query_string = ''; 
  31.  
  32. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request); 
  33. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request); 
  34. $request = ltrim($request, '/'); 
  35.  
  36. $base = trailingslashit( get_bloginfo( 'url' ) ); 
  37.  
  38. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) ) 
  39. $base .= $wp_rewrite->index . '/'; 
  40.  
  41. if ( $pagenum > 1 ) { 
  42. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' ); 
  43.  
  44. $result = $base . $request . $query_string; 
  45.  
  46. /** 
  47. * Filters the page number link for the current request. 
  48. * @since 2.5.0 
  49. * @param string $result The page number link. 
  50. */ 
  51. $result = apply_filters( 'get_pagenum_link', $result ); 
  52.  
  53. if ( $escape ) 
  54. return esc_url( $result ); 
  55. else 
  56. return esc_url_raw( $result );