WordPress添加Canonical标签

在网站优化上为页面设置canonical标签这是必做的一项,该标签的作用应该大家都有所了解,wordpress程序在文章页也提供的有canonical标签,但是功能上不太完善,现在网上关于设置canonical标签的帖子也非常多,但是很多都是过时的或者只能为首页,分类页,文章也设置,像作者页面,归档页面还有标签页面就没有设置的方法了,今天我给大家分享一段布置简单,功能全面的代码。

什么是canonical标签

引用:rel=”canonical” 这个标签已经推出很久了,canonical 是 Google、雅虎、微软等搜索引擎一起推出的一个标签,它的主要作用是用来解决由于网址形式不同内容相同而造成的内容重复问题。这个标签对搜索引擎作用非常大,简单的说它可以让搜索引擎只抓取你想要强调的内容。(百度目前也支持这个标签)

/* wordpress中使用canonical标签

/* ——————————– */

function cx_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 ”;

}

add_action(‘wp_head’, ‘cx_archive_link’);

添加与检查

把上述代码放到模板文件“functions.php”中,保存上传即可,最后在来检查下代码到底有没有安装成功并启用,我们打开浏览器插件“open seo stats”,(没有该插件的请移步至:https://www.qicaiyun.top/share/298.html 下载)如下图查看:
WordPress添加Canonical标签


七彩云博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress添加Canonical标签

您必须 登录 才能发表评论!

(6)个小伙伴在吐槽
  1. Sud Treatment http://aaa-rehab.com Drug Rehab Centers Near Me Inpatient Substance Abuse Treatment https://sellatease.com/user/profile/68511
    Medicaid Substance Abuse Treatment2020-04-01 04:16 Windows 10 | Chrome 66.0.3359.170
  2. Outpatient Substance Abuse Program http://aaa-rehab.com Drug Rehab Near Me Drug & Alcohol Treatment Centers http://airguns-club.ir/member.php?action=profile&uid=293
    Drug And Alcohol Rehab2020-03-31 12:04 Windows 10 | Chrome 68.0.3440.106
  3. Drug Rehabilitation Centers Near Me http://aaa-rehab.com Drug Rehab Centers Court Ordered Rehab https://www.cartoonmovement.com/p/156414
    Alcohol Treatment Facilities Near Me2020-03-30 19:45 Windows 10 | Chrome 67.0.3396.99
  4. Local Alcohol Treatment Centers http://aaa-rehab.com Alcohol Rehab Near Me Outpatient Alcohol Rehab http://yunosti2.ru/user/rehabcenters/
    Bay Cove Methadone Clinic2020-03-30 04:25 Windows 10 | Chrome 67.0.3396.99
  5. Residential Substance Abuse Treatment http://aaa-rehab.com Alcohol Rehab Centers http://aaa-rehab.com Residential Drug Treatment Programs Near Me http://aaa-rehab.com
    Residential Alcohol Rehab Centers2020-03-26 07:41 Windows 8.1 | Chrome 66.0.3359.181
  6. Long time reader, first time commenter -- so, thought I'd drop a comment..-- and at the same time ask for a favor.Your wordpress site is very simplistic - hope you don't mind me asking what theme you're using? (and don't mind if I steal it?:P)I just launched my small businesses site --also built in wordpress like yours-- but the theme slows (!) the site down quite a bit.In case you have a minute, you can find it by searching for "royal cbd" on Google (would appreciate any feedback)Keep up the good work-- and take care of yourself during the coronavirus scare!~Justin
    Justin Hamilton2020-03-17 20:55 Windows 8 | Chrome 64.0.3282.186