On this page
get_pagenum_link( int $pagenum = 1, bool $escape = true ): string
Retrieves the link for a page number.
Parameters
$pagenumint Optional-
Page number.
Default:
1 $escapebool Optional-
Whether to escape the URL for display, with esc_url() . Defaults to true.
Otherwise, prepares the URL with sanitize_url() .Default:
true
Return
string The link URL for the given page number.
Source
File: wp-includes/link-template.php. View all references
function get_pagenum_link( $pagenum = 1, $escape = true ) {
global $wp_rewrite;
$pagenum = (int) $pagenum;
$request = remove_query_arg( 'paged' );
$home_root = parse_url( home_url() );
$home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : '';
$home_root = preg_quote( $home_root, '|' );
$request = preg_replace( '|^' . $home_root . '|i', '', $request );
$request = preg_replace( '|^/+|', '', $request );
if ( ! $wp_rewrite->using_permalinks() || is_admin() ) {
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $pagenum > 1 ) {
$result = add_query_arg( 'paged', $pagenum, $base . $request );
} else {
$result = $base . $request;
}
} else {
$qs_regex = '|\?.*?$|';
preg_match( $qs_regex, $request, $qs_match );
if ( ! empty( $qs_match[0] ) ) {
$query_string = $qs_match[0];
$request = preg_replace( $qs_regex, '', $request );
} else {
$query_string = '';
}
$request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request );
$request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request );
$request = ltrim( $request, '/' );
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) {
$base .= $wp_rewrite->index . '/';
}
if ( $pagenum > 1 ) {
$request = ( ( ! empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $pagenum, 'paged' );
}
$result = $base . $request . $query_string;
}
/**
* Filters the page number link for the current request.
*
* @since 2.5.0
* @since 5.2.0 Added the `$pagenum` argument.
*
* @param string $result The page number link.
* @param int $pagenum The page number.
*/
$result = apply_filters( 'get_pagenum_link', $result, $pagenum );
if ( $escape ) {
return esc_url( $result );
} else {
return sanitize_url( $result );
}
}
Hooks
- apply_filters( 'get_pagenum_link',
string $result ,int $pagenum ) -
Filters the page number link for the current request.
Related
Uses
| Uses | Description |
|---|---|
| sanitize_url() wp-includes/formatting.php | Sanitizes a URL for database or redirect usage. |
| remove_query_arg() wp-includes/functions.php | Removes an item or items from a query string. |
| user_trailingslashit() wp-includes/link-template.php | Retrieves a trailing-slashed string if the site is set for adding trailing slashes. |
| WP_Rewrite::using_index_permalinks() wp-includes/class-wp-rewrite.php | Determines whether permalinks are being used and rewrite module is not enabled. |
| WP_Rewrite::using_permalinks() wp-includes/class-wp-rewrite.php | Determines whether permalinks are being used. |
| esc_url() wp-includes/formatting.php | Checks and cleans a URL. |
| trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
| get_bloginfo() wp-includes/general-template.php | Retrieves information about the current site. |
| is_admin() wp-includes/load.php | Determines whether the current request is for an administrative interface page. |
| add_query_arg() wp-includes/functions.php | Retrieves a modified URL query string. |
| home_url() wp-includes/link-template.php | Retrieves the URL for the current site where the front end is accessible. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| paginate_links() wp-includes/general-template.php | Retrieves paginated links for archive post pages. |
| get_next_posts_page_link() wp-includes/link-template.php | Retrieves the next posts page link. |
| get_previous_posts_page_link() wp-includes/link-template.php | Retrieves the previous posts page link. |
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_pagenum_link