昨天小編寫了一篇關於 WordPress 的 canonical 標籤的文章,不知道大家有沒有發現在百度知道的原始碼截圖中發現這麼一段

<meta name="robots" content="noindex,follow" />

那麼這段程式碼的作用是什麼呢?據 robotstxt.org 官方網站解釋,這段程式碼作用是遮蔽搜尋引擎對本頁面的收錄但是允許抓取該頁面上的連結。那麼為什麼百度知道的 SEO 工作人員會把這麼一條 meta 標籤寫在頁面上呢?經過小編的觀察發現百度知道所有的動態連結頁面上都會包含一條這樣的 meta 標籤,但是進入該頁面的偽靜態連結卻不會出現該 meta 標籤,那麼這樣看來百度知道官方的 SEOER 意圖很明顯啊,是為了減少不必要的收錄,來集中網站的權重。

好了接下來說正題,不知道大家發現了沒有,WordPress 的分類目錄的分頁以及標籤頁的分頁其實在搜尋引擎中排名並不好,而且也沒什麼實質性內容,那麼我們可以透過

<meta name="robots" content="noindex,follow" />

標籤來進行遮蔽以達到減少不必要的收錄集中網站權重的效果,實現方法,開啟主題的頭部檔案 (一般情況下為 header.php) 在其中新增以下程式碼:

<?php
if(is_home()) { ?>
<?php $paged = get_query_var('paged');
if ( $paged > 1 ) echo'<meta name="robots" content="noindex,follow" />'; ?>
<?php } ?>
<?php
if(is_category()) { ?>
<?php $paged = get_query_var('paged');
if ( $paged > 1 ) echo'<meta name="robots" content="noindex,follow" />'; ?>
<?php } ?>
<?php
if(is_tag()) { ?>
<?php $paged = get_query_var('paged');
if ( $paged > 1 ) echo'<meta name="robots" content="noindex,follow" />'; ?>
<?php }?>

做完了網站收錄處理,接下來在用 nofollow 屬性減少下頁面權重流失集中內頁權重,程式碼如下:

//閱讀更多連結新增 nofollow
add_filter('the_content_more_link','add_nofollow_to_link', 0);
function add_nofollow_to_link($link) {
return str_replace('<a', '<a rel="nofollow"', $link);
}
//標籤雲新增 nofollow
add_filter('wp_tag_cloud', 'cis_nofollow_tag_cloud');
function cis_nofollow_tag_cloud($text) {
    return str_replace('<a href=', '<a rel="nofollow" href=',$text);
}
//內容頁 tag 連結新增 nofollow
add_filter('the_tags', 'cis_nofollow_the_tag');
function cis_nofollow_the_tag($text) {
    return str_replace('rel="tag"', 'rel="tag nofollow"', $text);
}
//作者歸檔連結新增 nofollow
add_filter('the_author_posts_link', 'cis_nofollow_the_author_posts_link');
function cis_nofollow_the_author_posts_link ($link) {
return str_replace('</a><a href=', '<a rel="nofollow" href=',$link);
}

//訪客評論連結新增 nofollow
add_filter('comments_popup_link_attributes', 'cis_nofollow_comments_popup_link_attributes');
function cis_nofollow_comments_popup_link_attributes () {
echo 'rel="nofollow"';
}

以上程式碼加到 functions.php 檔案即可。