很久沒寫 WordPress 的 seo 教程了,難得小編今天有閒空正好更上一貼。百度知道相信廣大站站一定非常熟悉,作為百度旗下第一問答平台其 SEO 做的非常好,而且百度知道本身在各大搜索引擎的權重都非常之高。百度知道在廣大站長的眼中也是一個非常好的外鏈以及網站的宣傳平台,但是最近在百度知道的廣告越來越難發了。嘿嘿差點跑題了,先上一張截圖:

對沒錯就是 rel="canonical" 這個標籤,我們來看看百度官方中是怎麼解釋 canonical 標籤的作用:
Canonical 標籤有什麼作用?
對一組內容完全相同或高度相似的網頁,通過使用 Canonical 標籤可以告訴搜索引擎哪個頁面為規範的網頁,能夠規範網址並避免搜索結果中出現多個內容相同或相似的頁面,幫助解決重複內容的收錄問題,避免網站相同內容網頁的重複展示及權重的分散,提升規範網頁的權重,優化規範網頁的排名。 《百度站長平台》
如何在 WordPress 中使用 canonical 標籤呢?方法如下:
首先在 functions.php 文件中添加分類目錄分頁鏈接獲取函數,代碼如下:
function wxd_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;
}
然後打開主題的頭部文件 (一般情況下為 header.php) 在其中添加以下代碼:
<?php
if(is_home()) { ?>
<link rel="canonical" href="<?php%20echo%20wxd_archive_link();?>"/>
<?php } ?>
<?php
if(is_category()) { ?>
<link rel="canonical" href="<?php%20echo%20wxd_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%20wxd_archive_link();?>"/>
<?php }?>
這樣就實現了在 WordPress 網站上添加了 canonical 標籤,集中網站權重。