如何为WordPress导航菜单、标签、出站等链接添加nofollow标签属性
- 时间:2020-05-17 12:22:02
- 分类:网络文摘
- 阅读:152 次
在使用wordpress建设网站时,实际上只需要搜索引擎抓取文章链接就可以了,像站内导航、标签、站外等链接过多的被收录,反而会分散网站权重,尤其是文章、页面或评论中的外部出站链接如果失效了还会产生垃圾链接,想要解决这个问题,可以为这些链接添加 nofollow 属性。
nofollow 是一个HTML标签的属性值,由谷歌开创,其作用是“反垃圾链接”,目前已被百度、必应等各大搜索引擎所广泛支持。站长可以通过为链接添加nofollow属性,指示搜索引擎不要追踪(即抓取)网页上的带有nofollow属性的链接,禁止蜘蛛爬行和传递权重,以减少这些链接分散网站权重,这对于网站的seo建设还是很有必要的。
那么在wordpress中应该如何使用nofollow标签属性呢,下面就具体介绍一下:
一、为WordPress导航菜单链接添加 nofollow 属性
1、登陆wordpress后台,进入“外观 > 菜单”页面,点击右上角“显示选项”,在“链接关系(XFN)”处打勾。

2、接下来你就可以在菜单结构中看到“链接关系(XFN)”选项了,在此处添加 nofollow ,然后保存即可。

二、为WordPress标签链接添加 nofollow 属性
添加如下代码到wordpress主题的functions.php文件中:
- function cx_tags() {
- $posttags = get_the_tags();
- if ($posttags) {
- foreach($posttags as $tag) {
- echo '<a class="tag-link' . $tag->term_id . '" href="http://uuxn.com/wordpress-links-add-rel-nofollow/'.get_tag_link($tag).'" rel="nofollow">'.$tag->name.'</a>';
- }
- }
- }
然后用
- <?php cx_tags(); ?>
替换主题模板中标签标准函数:
- <?php the_tags(); ?>
说明:你也可以在 rel="nofollow" 后面添加 target="_blank" 实现在新窗口打开链接。
三、为WordPress文章和页面中的出站链接添加 nofollow 属性
添加如下代码到wordpress主题的functions.php文件中:
- add_filter( 'the_content', 'cn_nf_url_parse');
- function cn_nf_url_parse( $content ) {
- $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
- if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
- if( !emptyempty($matches) ) {
- $srcUrl = get_option('siteurl');
- for ($i=0; $i < count($matches); $i++)
- {
- $tag = $matches[$i][0];
- $tag2 = $matches[$i][0];
- $url = $matches[$i][0];
- $noFollow = '';
- $pattern = '/target\s*=\s*"\s*_blank\s*"/';
- preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
- if( count($match) < 1 )
- $noFollow .= ' target="_blank" ';
- $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
- preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
- if( count($match) < 1 )
- $noFollow .= ' rel="nofollow" ';
- $pos = strpos($url,$srcUrl);
- if ($pos === false) {
- $tag = rtrim ($tag,'>');
- $tag .= $noFollow.'>';
- $content = str_replace($tag2,$tag,$content);
- }
- }
- }
- }
- $content = str_replace(']]>', ']]>', $content);
- return $content;
- }
说明:如果已经手动给出站链接添加了 rel="nofollow" 或 target="_blank" 则不会重复添加。
此外,你也可以使用wordpress插件 Nofollow for external link 来实现为出站链接添加 nofollow 属性。
下载地址:http://wordpress.org/plugins/nofollow-for-external-link/
四、为WordPress评论中的出站链接添加 nofollow 属性
添加如下代码到wordpress主题的functions.php文件中:
- add_filter('comment_text', 'auto_nofollow'); //nofollow评论内容的站外链接
- function auto_nofollow($content) {
- //return stripslashes(wp_rel_nofollow($content));
- return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
- }
- function auto_nofollow_callback($matches) {
- $link = $matches[0];
- $site_link = get_bloginfo('url');
- if (strpos($link, 'rel') === false) {
- $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
- } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
- $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
- }
- return $link;
- }
五、出于特殊需要,你可能想为某个WordPress分类下文章的所有链接添加nofollow属性
添加如下代码到wordpress主题的functions.php文件中:
- function nofollow_cat_posts($text) {
- global $post;
- if( in_category(1) ) { // 修改这里的分类ID
- $text = stripslashes(wp_rel_nofollow($text));
- }
- return $text;
- }
- add_filter('the_content', 'nofollow_cat_posts');
以上就是关于在WordPress中为相关链接添加nofollow属性的介绍,你可以根据自己的需要进行选择。
推荐阅读:The Overlapping Rectangles using CSS and Javascript How to Count the Distinct Pairs using HashMap? Blogger Jailed For Pokemon Go Gets Even More Trouble Dead Simple Ways to Keep Your Best Blogging Ideas From Slipping The Fear of Blogging is Real – Here’s How to Overcome It Influential Cybersecurity Blogger Gets Digitally Attacked Building Relationships with Your Influencers Authorities In Vietnam Arrest Top Blogger For One Criticizing Co The Top Health Bloggers You Should Be Following Mashable Blogger: Owning a Samsung Galaxy Note 7 is Safer Than G
- 评论列表
-
- 添加评论