返回介绍

get_pagenum_link()

发布于 2017-09-10 23:41:38 字数 4087 浏览 1296 评论 0 收藏 0

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

Retrieves the link for a page number.


description


参数

$pagenum

(int) (Optional) Page ID.

Default value: 1

$escape

(bool) (Optional) Whether to escape the URL for display, with esc_url(). Defaults to true. Otherwise, prepares the URL with esc_url_raw().

Default value: true


返回值

(string) The link URL for the given page number.


源代码

File: wp-includes/link-template.php

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
	 *
	 * @param string $result The page number link.
	 */
	$result = apply_filters( 'get_pagenum_link', $result );

	if ( $escape )
		return esc_url( $result );
	else
		return esc_url_raw( $result );
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: esc_url()
  • wp-includes/formatting.php: esc_url_raw()
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/general-template.php: get_bloginfo()
  • wp-includes/load.php: is_admin()
  • wp-includes/functions.php: remove_query_arg()
  • wp-includes/functions.php: add_query_arg()
  • wp-includes/link-template.php: home_url()
  • wp-includes/link-template.php: get_pagenum_link
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::using_index_permalinks()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::using_permalinks()
  • Show 8 more uses Hide more uses

Used By

  • wp-includes/general-template.php: paginate_links()
  • wp-includes/link-template.php: get_next_posts_page_link()
  • wp-includes/link-template.php: get_previous_posts_page_link()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文