You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress中为指定链接(site.com)添加rel="nofollow"?(PHP/JQuery实现)

给WordPress中指向site.com的链接添加rel="nofollow"的两种实现方法

Hey there! Let's cover two reliable ways to add rel="nofollow" to links pointing to site.com on your WordPress site—one server-side with PHP (the better pick for SEO) and one client-side with jQuery (a quick fix if you can't mess with theme files right now).


方法1:PHP后端实现(推荐,SEO友好)

Since search engines crawl the server-rendered content of your site, this approach ensures the nofollow attribute is present from the get-go, which is ideal for SEO. Here are two common scenarios:

场景1:处理文章/页面内容里的链接

Use the the_content filter to scan and modify links in your post/page content:

function add_nofollow_to_specific_link($content) {
    // Match links pointing to site.com (with or without www/http(s))
    $pattern = '/<a(.*?)href=["\'](https?:\/\/)?(www\.)?site\.com(["\'])(.*?)>/i';
    
    // Handle links that already have a rel attribute
    $content = preg_replace($pattern, function($matches) {
        $relAttr = '';
        // Check if rel already exists in the link attributes
        if (strpos($matches[1], 'rel="') !== false) {
            // Append nofollow to existing rel value
            $matches[1] = preg_replace('/rel="([^"]*)"/i', 'rel="$1 nofollow"', $matches[1]);
        } else {
            // Add rel="nofollow" if no existing rel attribute
            $matches[1] .= ' rel="nofollow"';
        }
        return '<a' . $matches[1] . 'href="' . $matches[2] . $matches[3] . 'site.com' . $matches[4] . '"' . $matches[5] . '>';
    }, $content);
    
    // Clean up any duplicate spaces in rel attributes
    $content = preg_replace('/rel="(.*?)(?:\s+nofollow)+(.*?)"/i', 'rel="$1 nofollow $2"', $content);
    $content = preg_replace('/rel="\s*(.*?)\s*"/i', 'rel="$1"', $content);
    
    return $content;
}
add_filter('the_content', 'add_nofollow_to_specific_link');

Drop this code into your theme's functions.php file, or use a custom plugin to add it (safer, since theme updates won't wipe your changes).

场景2:处理导航菜单里的链接

If you need to target links in your WordPress navigation menus, use the wp_nav_menu_objects filter:

function add_nofollow_to_menu_link($items) {
    foreach ($items as $item) {
        // Check if the menu item's URL contains site.com
        if (strpos($item->url, 'site.com') !== false) {
            if (!empty($item->attr_title)) {
                // Append nofollow to existing title/attributes
                if (strpos($item->attr_title, 'rel="') !== false) {
                    $item->attr_title = preg_replace('/rel="([^"]*)"/i', 'rel="$1 nofollow"', $item->attr_title);
                } else {
                    $item->attr_title .= ' rel="nofollow"';
                }
            } else {
                // Add rel attribute directly if no existing attributes
                $item->attr_title = 'rel="nofollow"';
            }
        }
    }
    return $items;
}
add_filter('wp_nav_menu_objects', 'add_nofollow_to_menu_link');

方法2:jQuery前端实现(临时快速方案)

This method modifies links after the page loads, so note that some search engines might not pick up the nofollow attribute (since they crawl the raw HTML). Use this only if you can't access backend files right now:

jQuery(document).ready(function($) {
    // Target all links that include site.com in their href
    $('a[href*="site.com"]').each(function() {
        let currentRel = $(this).attr('rel') || '';
        // Only add nofollow if it's not already present
        if (!currentRel.includes('nofollow')) {
            // Trim to avoid extra spaces if currentRel was empty
            $(this).attr('rel', `${currentRel.trim()} nofollow`.trim());
        }
    });
});

Add this script right before the closing </body> tag in your theme's footer.php file, or enqueue it properly using WordPress's wp_enqueue_script function for better practice.


内容的提问来源于stack exchange,提问作者p47ri0t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:26:01