nofollow屬性是告訴搜索引擎不傳權重過去,但WordPressk中如果我們要nofollow屬性就需要手工加了,現在我來告訴大家利用 Nofollow for external link就可以自動給文章添加nofollow屬性了.
直接安裝啟用 Nofollow for external link 插件,或者將下面的代碼添加到當前主題的 functions.php 文件即可.
實例代碼如下:
- add_filter( 'the_content', 'cn_nf_url_parse');
- 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;
- }
最終效果:自動給文章/頁面的站外鏈接添加nofollow屬性(rel=”nofollow”),并且在新窗口打開這些鏈接(即添加 target=”_blank”屬性),如果已經手動給鏈接添加了 rel=”dofollow”,就不會添加 rel=”nofollow”,如果手動添加了 target=”_blank”,就不會重復添加.
為指定分類的所有鏈接添加nofollow屬性,那你可以將下面的代碼添加到主題的 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');
新聞熱點
疑難解答
圖片精選