WordPress内链的好坏就不用说了,它可以让百度蜘蛛在你的网站爬行更多的地方,利于SEO,这里就不过多介绍了,追格小编直奔主题WordPress怎么给文章关键词标签自动添加内链。
在WordPress主题目录下的functions.php文件中添加以下代码就可以实现:
/WordPress 文章关键词自动内链
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tag_link($content){
$match_num_from = 1; //一个标签少于几次不添加链接
$match_num_to = 1; //一个标签至多添加几次链接
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "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);
//不添加链接代码
$content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)<\/pre>(</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;
}
add_filter('the_content','tag_link',1);
相关推荐
WordPress用代码实现自动为文章添加标签
-
Loco Translate 一款专为WordPress设计的翻译插件WordPress Loco Translate是一款专为WordPress用户设计的翻译插件,它简化了主题和插件的多语言翻译过程。
-
追格小程序V2.5.0更新发布追格小程序V2.5.0版本的更新,我们没给大家添太多新花样,主要是把“内功”练好了。我们认真检查了上一版里那些组件之间的小摩擦,一点点地调整、优化,搞定了超过40多个小地方。这样一来,小程序跑起来就更顺畅了,大家用起来也会感觉更舒服、更顺手。这次虽然没大张旗鼓,但确实是让产品站得更稳、更可靠了。
-
WordPress后台待审文章显示气泡提示的方法之前小编分享过一篇《WordPress 后台菜单添加 badge-红点提示(气泡通知)》的文章,今天我简单说说待审文章(为全部文章类型显示待审角标提示)的气泡提示如何操作。
-
2024年国庆放假通知追格2024年国庆节放假有关事宜通知如下
-
FluentSMTP一款功能强大且免费的WordPress SMTP插件FluentSMTP是一款功能强大且免费的SMTP插件,它支持为WordPress配置多个SMTP发送服务器。
-
追格小程序V2.5.8更新发布追格小程序是一款采用积木式理念的微信小程序构建框架,它巧妙融合了Uniapp与WordPress技术,打造出一个开源且无加密限制的开发环境。
暂无评论,抢个沙发...