是不是发现无论网站发了多少篇文章,搜索引擎还是不怎么亲睐,收录个位数,就是因为内容不符合搜索引擎的口味,网站内链外链都要合理利用,才能得到更多的收录。我们是本着能不用插件就不用插件的前提,保证质量的同时还能兼顾页面打开速度,以下是两种市面流行的纯代码加标签内链接的方法,大家按需要取用。
注意:下面是两个版本的代码,根据自己的需要选择其中一种添加即可!
不用插件给文章加入标签内链方法一:
所有标签都加链接,不限定次数,如果标签出现10次,就加十次内链接。
function wpkj_auto_add_tag_link($content){ $limit = 10; // 设置同一个标签添加几次链接 $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $link = get_tag_link($tag->term_id); $keyword = $tag->name; $cleankeyword = stripslashes($keyword); $url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')).'">'.addcslashes($cleankeyword, '$').'</a>'; $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s'; $content = preg_replace($regEx,$url,$content,$limit); } } return $content; } add_filter( 'the_content', 'wpkj_auto_add_tag_link', 1 );
不用插件给文章加入标签内链方法二:
无论出现多少次标签文字,都只添加3次内链,具体数字可以在代码里修改对应内容
/*文章标签加内链 START*/ /** * wordpress不用插件给文章加入标签内链 * https://flying.wang/454.html */ $match_num_from = 1; // 一个标签在文章中出现少于多少次不添加链接 $match_num_to = 3; // 一篇文章中同一个标签添加几次链接 add_filter('the_content','wpkj_tag_link',1); //按长度排序 function wpkj_tag_sort($a, $b){ if ( $a->name == $b->name ) return 0; return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } //为符合条件的标签添加链接 function wpkj_tag_link($content){ global $match_num_from,$match_num_to; $posttags = get_the_tags(); if ($posttags) { usort($posttags, "wpkj_tag_sort"); foreach($posttags as $tag) { $link = get_tag_link($tag->term_id); $keyword = $tag->name; //链接的代码 $cleankeyword = stripslashes($keyword); $url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('查看与【%s】相关的文章'))."\""; $url .= ' target="_blank"'; $url .= ">".addcslashes($cleankeyword, '$')."</a>"; $limit = rand($match_num_from,$match_num_to); //不链接的代码 $ex_word = ''; $case = ''; $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content); $cleankeyword = preg_quote($cleankeyword,'\''); $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content); } } return $content; } /*文章标签加内链 END*/
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)