昨天小編寫了一篇關於 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 文件即可。