返回介绍

wp_get_canonical_url()

发布于 2017-09-11 12:01:03 字数 2956 浏览 1136 评论 0 收藏 0

wp_get_canonical_url( int|WP_Post $post = null )

Returns the canonical URL for a post.


description

When the post is the same as the current requested page the function will handle the pagination arguments too.


参数

$post

(int|WP_Post) (Optional) Post ID or object. Default is global $post.

Default value: null


返回值

(string|false) The canonical URL, or false if the post does not exist or has not been published yet.


源代码

File: wp-includes/link-template.php

function wp_get_canonical_url( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	if ( 'publish' !== $post->post_status ) {
		return false;
	}

	$canonical_url = get_permalink( $post );

	// If a canonical is being generated for the current page, make sure it has pagination if needed.
	if ( $post->ID === get_queried_object_id() ) {
		$page = get_query_var( 'page', 0 );
		if ( $page >= 2 ) {
			if ( '' == get_option( 'permalink_structure' ) ) {
				$canonical_url = add_query_arg( 'page', $page, $canonical_url );
			} else {
				$canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
			}
		}

		$cpage = get_query_var( 'cpage', 0 );
		if ( $cpage ) {
			$canonical_url = get_comments_pagenum_link( $cpage );
		}
	}

	/**
	 * Filters the canonical URL for a post.
	 *
	 * @since 4.6.0
	 *
	 * @param string  $canonical_url The post's canonical URL.
	 * @param WP_Post $post          Post object.
	 */
	return apply_filters( 'get_canonical_url', $canonical_url, $post );
}

更新日志

Versiondescription
4.6.0Introduced.

相关函数

Uses

  • wp-includes/link-template.php: get_canonical_url
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/query.php: get_queried_object_id()
  • wp-includes/query.php: get_query_var()
  • wp-includes/functions.php: add_query_arg()
  • wp-includes/link-template.php: get_comments_pagenum_link()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/post.php: get_post()
  • Show 6 more uses Hide more uses

Used By

  • wp-includes/link-template.php: rel_canonical()

User Contributed Notes

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

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

发布评论

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