前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress SEO优化:纯代码添加canonical标签

WordPress SEO优化:纯代码添加canonical标签

原创
作者头像
Yangsh888
发布2022-08-15 22:07:25
4920
发布2022-08-15 22:07:25
举报
文章被收录于专栏:Yangsh888的专栏Yangsh888的专栏

为网站添加添加canonical标签是SEO优化中非常重要的一步,rrel="canonical"可以解决因网址不同但内容重复,从而造成权重分散的问题,目前百度、Google、雅虎、微软等搜索引擎都已支持此标签。

例子演示

https://abc.com/
https://abc.com/page/2

为了避免首页权重的分散,应该通过rrel="canonical"标签告诉搜索引擎,这两个页面的权重要集中在第一个网址。

<link rel="canonical" href="https://abc.com/" />

具体的做法是将上述标签添加至这两个页面的/head标签前。

添加方式

分享2种纯代码为 WordPress 首页、分类、标签和文章页自动添加 canonical 标签的方法,将下面任意一份代码添加到 WordPress 主题 functions.php 文件中。

方法一

//纯代码添加canonical标签,集中页面权限
remove_action( 'wp_head', 'rel_canonical' );
function cccitu_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_single() ) {
                $link = get_permalink( $id );
        } 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' );
                }
        }
        echo '<link rel="canonical" href="'.$link.'" />';
}
add_action('wp_head', 'cccitu_archive_link');

方法二

//纯代码添加canonical标签,集中页面权限
remove_action( 'wp_head', 'rel_canonical' );
function cccitu_rel_canonical() {
    global $post;
    if (is_single() || is_page()) {
    echo "<link rel=\"canonical\" href=\"" . get_permalink( $post->ID ) . "\" />\n";
    }
    if (is_home()) {
    echo "<link rel=\"canonical\" href=\"".home_url("/")."\" />\n";
    }
    if (is_category() || is_category() && is_paged()) {
    echo "<link rel=\"canonical\" href=\"".get_category_link(get_query_var('cat'))."\" />\n";
    }
    if (is_tag() || is_tag() && is_paged()) {
    echo "<link rel=\"canonical\" href=\"".get_term_link(get_query_var('tag'), 'post_tag')."\" />\n";
    }
    if (is_search() || is_search() && is_paged()) {
    echo "<link rel=\"canonical\" href=\"".get_search_link(get_query_var('search'))."\" />\n";
    }
    if (is_author()) {
    echo "<link rel=\"canonical\" href=\"".get_option('home')."\" />\n";
    }
    if (is_date()) {
    echo "<link rel=\"canonical\" href=\"".get_option('home')."\" />\n";
    }
    }
add_action('wp_head', 'cccitu_rel_canonical');

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 例子演示
  • 添加方式
    • 方法一
      • 方法二
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档