get_pagenum_link()WP 1.5.0

Retrieves the link for a page number.

Hooks from the function

Return

String. The link URL for the given page number.

Usage

get_pagenum_link( $pagenum, $escape );
$pagenum(int)
Page number.
Default: 1
$escape(true|false)
Whether to escape the URL for display, with esc_url(). Otherwise, prepares the URL with sanitize_url().
Default: true

Examples

0

#1 Example of using a link on different pages

echo get_pagenum_link( 4 );

// On Home Page:
// returns: http://example.com/page/4

// On Page: http://example.com/article
// returns: http://example.com/article/page/4
0

#2 Change the pagination link

Let's say on the home page is_home() we need to add /article/ to the pagination links, i.e. from http://example.com/page/1 we need to make http://example.com/article/page/1

add_filter( 'get_pagenum_link', 'edit_paginate_url_for_home_page' );

function edit_paginate_url_for_home_page( $url ){

	if( ! is_home() || is_paged() )
		return;

	static $site = '';
	$site || $site = home_url();

	$url  = preg_replace( "@^$site/(.*)@", "$site/article/\1", $url );

	return $url;
}

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 1.5.0 Introduced.

get_pagenum_link() code WP 6.1.1

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 );
	}
}