WordPress SEO:文章页添加Canonical标签,防止页面重复收录

2023-04-1812:54:45网站运营与SEO优化Comments1,677 views字数 1697阅读模式

是否有看到网站有收录页面的被重复收录,而且有的同一篇文章有收录的是伪静态页面,有收录的是动态页面。这样重复收录理论上对于SEO是不利的。我们最好能实现统一的唯一的收录页面,使得权限集中在一个URL页面中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/35178.html

WordPress SEO:文章页添加Canonical标签,防止页面重复收录

这里,我们可以在WordPress文章页面模板中添加 Canonical 标签实现唯一的URL。当然,如果我们有用到类似的插件也可以实现,这里我们不用插件,用代码实现。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/35178.html

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。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/35178.html

然后头部模板header.php中添加。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/35178.html

<?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 }?>

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/35178.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/youhua/35178.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/youhua/35178.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定