如何在 WordPress 中加入 noindex 標記?

本篇文章您將了解如何在 WordPress 中為特定頁面加入 noindex 標記,且無需透過外掛。只要五秒鐘,您就能輕鬆為網站加入 noindex 標記,禁止 Google 索引內容過少的頁面,提升優質內容的排名。

為何要設定 noindex 標記?

網站某些部分中的劣質內容,會對整個網站的排名造成影響,因此移除劣質網頁、將個別膚淺網頁的內容合併或改為更實用的網頁,或是將品質低劣的網頁移到其他網域,最終都有助於提升優質內容的排名。

資料來源: 優質網站建立方式的額外說明 – Google 搜尋中心網誌

「文章是否太過簡短、不切實際,或缺乏實用細節?」是 Google 在部落格中所提供的思考邏輯參考,因次禁止 Google 索引內容過少或是缺乏實用細節的頁面有助於改善網站在 Google 上的排名。

在 WordPress 的架構中,我認為有許多頁面不應該被 Google 索引,例如文章分類和標籤、作者、分頁、日期或是特定的頁面。這些頁面通常都有內容簡短與缺乏實用細節等特性,因此我認為在這些頁面中設定 noidex
來禁止Google 索引對於改善網站在 Google 上的排名是有幫助的。

WordPress 如何在不使用外掛的環境下,為特定的頁面設定「noindex」標記?

WordPress 有許多 SEO 外掛都有提供 noindex 標記設定的功能,例如 AIOSEO 或 Yoast SEO,雖然非常方便,但是這些外掛通常都會佔掉網站許多記憶體容量,也是造成網站讀取速度變慢的原因之一。

因此,我建議在 <head> 或是 function.php 中加入 noindex 標記,網站就可以在不影響讀取速度下,達到禁止被 Google 索引的目的。

【方法一】在 <head> 中加入 noindex 標記

<?php
    if( is_author() || is_date() || is_paged() || is_search() || is_category() || is_tag() ){
        echo '<meta name="robots" content="noindex,follow"/>';
    }
?/>

【方法二】在 functions.php 中加入 noindex 標記

function add_noindex() {
    if( is_author() || is_date() || is_paged() || is_search() || is_category() || is_tag() ){
        echo '<meta name="robots" content="noindex,follow"/>';
    }
}
add_action('wp_head', 'add_noindex');

【方法三】在 functions.php 中為「頁面」和「文章」設定 noindex 勾選功能

function add_noindex_meta_box() {
    add_meta_box(
        'noindex_meta_box',             
        'Noindex Pages',         
        'custom_noindex_setting', 
        ['page', 'post'],
        'side',
        'high'
    );
}
add_action('add_meta_boxes', 'add_noindex_meta_box');

function custom_noindex_setting($post) {
    $value = get_post_meta($post->ID, '_noindex_meta_key', true);
    wp_nonce_field('save_noindex_nonce', 'noindex_nonce');
    ?>
        <p>
            <label>
                <input type="checkbox" name="noindex_meta_checkbox" <?php checked($value, 'yes'); ?> />
                noindex
            </label>
        </p>
    <?php
}
function save_noindex_meta_box($post_id) {
    if (!isset($_POST['noindex_nonce']) || !wp_verify_nonce($_POST['noindex_nonce'], 'save_noindex_nonce'))
        return;
        
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
        
    if (!current_user_can('edit_post', $post_id))
        return;
        
    $chk = isset($_POST['noindex_meta_checkbox']) && $_POST['noindex_meta_checkbox'] ? 'yes' : 'no';
        update_post_meta($post_id, '_noindex_meta_key', $chk);
}
        
add_action('save_post', 'save_noindex_meta_box');
        
function add_noindex_if_checked(){
    if (is_singular(['page', 'post'])) {
        $noindex = get_post_meta(get_the_ID(), '_noindex_meta_key', true);
        if ($noindex === 'yes') {
            echo '<meta name="robots" content="noindex,follow">';
        }
    }
}
add_action('wp_head', 'add_noindex_if_checked');

WordPress 那些頁面要加入 noindex 標記?

  • is_author(): 是否為作者頁面
  • is_date(): 是否為日期頁面
  • is_paged(): 是否為分頁頁面
  • is_search(): 是否為搜索結果頁面
  • is_category(): 是否為分類頁面
  • is_tag(): 是否為標籤頁面

如果「作者頁面」的內容沒有很多,建議加入 noindex 禁止 Google 索引。「日期頁面」通常只根據日期對文章進行分類,並不提供額外的價值或獨特內容,因此建議加入 noindex 來禁止 Google
索引。「分頁頁面」和「搜索結果頁面」通常包含重複的內容,因此建議加入 noindex 來禁止 Google 索引。「分類頁面」和「標籤頁面」中若只有文章列表,而沒有詳細介紹分類,會建議加入 noindex 來禁止 Google
索引。

相反地,如果「分類頁面」和「標籤頁面」是網站經常被搜尋的關鍵字,例如「Apple Macbook」是經常被搜尋的「品牌+分類」的關鍵字,那麼「分類頁面」設定 index 來被 Goolge 索引是有非常大的益處。

重點整理

如果不想讓 WordPress 外掛而造成網站速度變慢,建議在 <head> 或是 function.php 中加入 noindex 標記。本篇文章提供了三種手動加入 noindex 的方法,您可以依照網站的設計風格,或是使用習慣來設定。