wp_get_canonical_url()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_get_canonical_url( int|WP_Post $post = null ): string|false
Описание
Если запись совпадает с текущей запрошенной страницей, функция также обработает аргументы постраничной навигации.
Оригинал (английский)
When the post is the same as the current requested page the function will handle the pagination arguments too.
Параметры
$post
int|WP_Post
необязательный
= null
Возвращаемое значение
string|false
Исходный код
wp-includes/link-template.php:4068
function wp_get_canonical_url( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( 'publish' !== get_post_status( $post ) ) {
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 ( get_queried_object_id() === $post->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 );
}
История изменений
| Версия | Описание |
|---|---|
| 4.6.0 | Introduced. |

