您需要替换默认值rel_canonical
执行此操作的函数:
function wpse_84647_rel_canonical() {
/* Only affect singular posts. Exit early if Yoast SEO is doing this for us */
if ( ! is_singular() || class_exists( \'WPSEO_Frontend\' ) )
return;
/* Get the post permalink */
$post = get_queried_object();
$link = trailingslashit( get_permalink( $post->ID ) );
/* Get the number of pages the post is split into */
$numpages = substr_count( $post->post_content, \'<!--nextpage-->\' ) + 1;
/* Get the current pagination page number */
$page = ( get_query_var( \'page\' ) ? get_query_var( \'page\' ) : 1 );
/* Add the page number if the post is paginated */
if ( $numpages && $page > 1 )
$canonical = user_trailingslashit( $link . $page );
else
$canonical = get_permalink( $post->ID );
/* Output the canonical link */
printf ( "\\n" . \'<link rel="canonical" href="%s" />\', $canonical );
/* Add the adjacent rel links */
if ( $page > 1 ) {
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0 );
/* Link to the previous adjacent post */
$prev = $page - 1;
if ( $prev > 1 )
printf ( "\\n" . \'<link rel="prev" href="%s" />\', user_trailingslashit( $link . $prev ) );
else
printf ( "\\n" . \'<link rel="prev" href="%s" />\', get_permalink( $post->ID ) );
}
if ( $page < $numpages ) {
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0 );
/* Link to the next adjacent post */
$next = $page + 1;
printf ( "\\n" . \'<link rel="next" href="%s" />\', user_trailingslashit( $link . $next ) );
}
print "\\n";
}
remove_action( \'wp_head\', \'rel_canonical\' );
add_action( \'wp_head\', \'wpse_84647_rel_canonical\', 9 );
这个新函数adds从URL获取分页页面,并将其添加到规范链接。它还将向相邻页面添加下一个和上一个链接,以便谷歌知道
此外,如果您使用Yoast的WordPress SEO插件,您将不需要此代码段,因为该插件为您处理规范链接。