WordPress SEO:在文章页面添加Canonical标签,防止重复页面录制
您有没有注意到网站上的录制页面被重复录制?此外,某些相同的文章包含伪静态页面,而另一些则包含动态页面。这种重复收录理论上对SEO是有害的。我们最好实现一个统一且唯一的记录页面,以便权利集中在一个URL页面上。 ![]()
这里我们可以在WordPress文章页面模板中添加Canonical标签来获取唯一的URL。如果我们使用类似的插件,我们当然也可以实现它们。这里我们不使用插件,而是使用代码来实现。
function v7v3_archive_link( $paged = true ) {
$link = false;
if ( is_front_page() ) {
$link = home_url( '/' );
} else if ( is_home() && "page" == get_option('show_on_front') ) {
$link = get_permalink( get_option( 'page_for_posts' ) );
} else if ( is_tax() || is_tag() || is_category() ) {
$term = get_queried_object();
$link = get_term_link( $term, $term->taxonomy );
} else if ( is_post_type_archive() ) {
$link = get_post_type_archive_link( get_post_type() );
} else if ( is_author() ) {
$link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );
} else if ( is_archive() ) {
if ( is_date() ) {
if ( is_day() ) {
$link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
} else if ( is_month() ) {
$link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
} else if ( is_year() ) {
$link = get_year_link( get_query_var('year') );
}
}
}
if ( $paged && $link && get_query_var('paged') > 1 ) {
global $wp_rewrite;
if ( !$wp_rewrite->using_permalinks() ) {
$link = add_query_arg( 'paged', get_query_var('paged'), $link );
} else {
$link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );
}
}
return $link;
}添加到此处的functions.php。
接下来,添加标头模板header.php。
<?php
if(is_home()) { ?>
<link rel="canonical" href="<?php%20echo%20v7v3_archive_link();?>"/>
<?php } ?>
<?php
if(is_category()) { ?>
<link rel="canonical" href="<?php%20echo%20v7v3_archive_link();?>"/>
<?php } ?>
<?php
if(is_single()) { ?>
<link rel="canonical" href="<?php%20the_permalink();%20?>"/>
<?php }?>
<?php
if(is_tag()) { ?>
<link rel="canonical" href="<?php%20echo%20v7v3_archive_link();?>"/>
<?php }?> 版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
code前端网