返回帖子的规范URL。
原型
wp_get_canonical_url( int|WP_Post $post = null )
描述
当帖子与当前请求的页面相同时,该函数也将处理分页参数。
参数
$post
(int|WP_Post)
(Optional)
邮政ID或对象。默认为全球$ post。
返回值
(string|false)
规范网址,如果帖子不存在或尚未发布,则为false。
源文件
路径:wp-includes/link-template.php
<?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 );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_get_canonical_url/