WordPress 博客经常会遇到评论的垃圾链接,需要给网站之外的链接添加 Nofollow 属性让网站权重不在流失,nofollow 是一个 HTML 标签的属性值,作用是告诉搜索引擎不要追踪此网页上的链接或不要追踪此特定链接,荒岛本次带来 WordPress 博客外链添加 nofollow 属性的教程,只要将下面的代码添加到主题的「functions.php」文件即可。
// 荒岛一座藏有宝藏的小岛 add_filter( 'the_content', 'add_nofollow_content' ); function add_nofollow_content($content) { $content = preg_replace_callback( '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i', function( $m ) { $site_link = get_option( 'siteurl' ); $site_link_other = get_option( 'siteurl' ); if (strpos( $m[1], $site_link ) === false && strpos( $m[1], $site_link_other ) === false ) return '<a href="'.$m[1].'" rel="external nofollow" target="_blank">'.$m[2].'</a>'; else return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>'; }, $content ); return $content; }宝藏内容来自荒岛-https://x-imagine.com/wordpress-external-link-nofollow-attribute.html宝藏内容来自荒岛-https://x-imagine.com/wordpress-external-link-nofollow-attribute.html
请按「Ctrl+D」收藏荒岛分享给好友,如您发现本文教程或宝藏链接已失效,请联系站长修正!
荒岛公众号
扫一扫关注

HDsoft27
公众号ID已复制,前往微信关注...
荒岛哔哩姬
扫一扫关注

ximagine
哔哩姬ID已复制,前往B站关注...