Z-Blog給文章所有的站外a鏈接添加nofollow的方法
- 2019 年 11 月 13 日
- 筆記
rel="nofollow"
屬性,高速搜索引擎,不要跟蹤該鏈接。在這我不就詳細介紹 nofollow 了,近段時間我會整理一些關於 nofollow 的資料。

Z-Blog 給文章中的站外鏈批量添加 nofollow 屬性,因為手動去加太費勁了,而且在網上也沒有找到相關教程,就研究了一下,寫了個 JS ,實現給 a 鏈接批量添加 nofollow。
首先找到文章的模板文件,我的是在 模板文件夾 /template/post-single.php
文件。
首先遍歷文章中的 a 鏈接:
$(".article-content a").each(function(){ var articleHref = $(this).attr("href"); })
然後,摘出主域名部分:(工作原理我會寫一篇文章具體分析)
var articleHref2 = articleHref.split('/')[2];
將摘出的域名與網站的域名進行對比,如果不同,則添加 nofollow 屬性。
if(articleHref2 != window.location.host){ $(this).attr("rel","external nofollow"); };
去除多餘的程式碼,最終程式碼如下:
<script> $(".article-content a").each(function(){ var articleHref = $(this).attr("href").split('/')[2]; if(articleHref != window.location.host){ $(this).attr("rel","external nofollow"); }; }) </script>
聲明:本文由w3h5原創,轉載請註明出處:《Z-Blog給文章所有的站外a鏈接添加nofollow的方法》 https://www.w3h5.com/post/211.html