:root{--puock-block-not-tran:100%}

WordPress添加Canonical标签

574次阅读
6 条评论

共计 1700 个字符,预计需要花费 5 分钟才能阅读完成。

在网站优化上为页面设置 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”,(没有该插件的请移步至: 下载)如下图查看:
WordPress 添加 Canonical 标签

正文完
 0
评论(6 条评论)
2020-04-01 04:16:10 回复

Sud Treatment http://aaa-rehab.com Drug Rehab Centers Near Me Inpatient Substance Abuse Treatment
https://sellatease.com/user/profile/68511

 Windows  Opera  美国俄亥俄亚马逊
Drug And Alcohol Rehab 评论达人 LV.1
2020-03-31 12:04:15 回复

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

 Windows  Opera  美国俄亥俄亚马逊
2020-03-30 19:45:58 回复

Drug Rehabilitation Centers Near Me http://aaa-rehab.com Drug Rehab Centers Court Ordered Rehab
https://www.cartoonmovement.com/p/156414

 Windows  Chrome  美国俄亥俄亚马逊
Bay Cove Methadone Clinic 评论达人 LV.1
2020-03-30 04:25:48 回复

Local Alcohol Treatment Centers http://aaa-rehab.com Alcohol Rehab Near Me Outpatient Alcohol Rehab
http://yunosti2.ru/user/rehabcenters/

 Windows  Chrome  美国俄亥俄亚马逊
Residential Alcohol Rehab Centers 评论达人 LV.1
2020-03-26 07:41:20 回复

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

 Windows  Chrome  美国俄亥俄亚马逊
Justin Hamilton 评论达人 LV.1
2020-03-17 20:55:12 回复

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

 Windows  Chrome  美国纽约纽约