服务跨境电商
助力企业出海

WordPress教程:DUX主题文章链接添加nofollow标签与新窗口打开

wordpress教程多少都是通用的,如何为文章外链添加target="_blank"rel="nofollow"标签呢,那么下面站长以国外主机测评使用的DUX主题测试;

1657675184 WordPress

具体方法:

  • 找到主题下的functions.php
  • 将以下代码增加到最后面:
复制// 自动给页面的站外链接添加nofollow属性和新窗口打开
add_filter( 'the_content', 'cn_nf_url_parse');
function cn_nf_url_parse( $content ) {
$regexp = "]*href=(/"??)([^/" >]*?)//1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !empty($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;
}

添加完即可看到效果。

说明:部分主题functions.php进行了加密,可以以添加到相应的主题中的说明文件下,比如DUX可以添加到functions-theme.php最后即可!

声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《WordPress教程:DUX主题文章链接添加nofollow标签与新窗口打开》
文章链接:https://www.zjcp.com/8863.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。