WordPress自動(dòng)給文章添加nofollow屬性的實(shí)現(xiàn)方法
本文實(shí)例講述了WordPress自動(dòng)給文章添加nofollow屬性的實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
nofollow屬性是告訴搜索引擎不傳權(quán)重過去,但WordPressk中如果我們要nofollow屬性就需要手工加了,現(xiàn)在我來告訴大家利用 Nofollow for external link就可以自動(dòng)給文章添加nofollow屬性了.
直接安裝啟用 Nofollow for external link 插件,或者將下面的代碼添加到當(dāng)前主題的 functions.php 文件即可.
實(shí)例代碼如下:
function cn_nf_url_parse( $content ) {
$regexp = "<as[^>]*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 = '/targets*=s*"s*_blanks*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rels*=s*"s*[n|d]ofollows*"/';
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;
}
最終效果:自動(dòng)給文章/頁面的站外鏈接添加nofollow屬性(rel=”nofollow”),并且在新窗口打開這些鏈接(即添加 target=”_blank”屬性),如果已經(jīng)手動(dòng)給鏈接添加了 rel=”dofollow”,就不會(huì)添加 rel=”nofollow”,如果手動(dòng)添加了 target=”_blank”,就不會(huì)重復(fù)添加.
為指定分類的所有鏈接添加nofollow屬性,那你可以將下面的代碼添加到主題的 functions.php 文件即可:
global $post;
if( in_category(1) ) { // 修改這里的分類ID
$text = stripslashes(wp_rel_nofollow($text));
}
return $text;
}
add_filter('the_content', 'nofollow_cat_posts');
希望本文所述對(duì)大家的WordPress建站有所幫助。
上一篇:WordPress給文章圖片自動(dòng)添加鏈接的方法
欄 目:WordPress
本文標(biāo)題:WordPress自動(dòng)給文章添加nofollow屬性的實(shí)現(xiàn)方法
本文地址:http://www.jygsgssxh.com/a1/WordPress/12684.html
您可能感興趣的文章
- 05-31wordpress修改后臺(tái)登錄地址教程
- 05-31WordPress自定義用戶頭像實(shí)現(xiàn)代碼
- 01-11修改wordpress上傳臨時(shí)目錄解決wordpress無法安裝插件包的方法
- 01-11WordPress增加文章排序方式
- 01-11自己做wordpress評(píng)論插件修改評(píng)論樣式(兩步美化評(píng)論內(nèi)容)
- 01-11wordpress get
- 01-11wordpress獲取文章評(píng)論數(shù)過濾掉作者代碼分享
- 01-11在wordpress文章末尾添加內(nèi)容的簡單方法
- 01-11不用wordpress插件顯示文章瀏覽統(tǒng)計(jì)功能代碼修改(原理分析)
- 01-11wordpress dynamic


閱讀排行
本欄相關(guān)
- 05-31wordpress修改后臺(tái)登錄地址教程
- 05-31WordPress自定義用戶頭像實(shí)現(xiàn)代碼
- 01-11WordPress增加文章排序方式
- 01-11修改wordpress上傳臨時(shí)目錄解決wordpre
- 01-11wordpress get
- 01-11自己做wordpress評(píng)論插件修改評(píng)論樣式
- 01-11在wordpress文章末尾添加內(nèi)容的簡單方
- 01-11wordpress獲取文章評(píng)論數(shù)過濾掉作者代
- 01-11wordpress dynamic
- 01-11不用wordpress插件顯示文章瀏覽統(tǒng)計(jì)功
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)


