wordpress 不使用插件让标签TAG内链

wordpress如何实现tag标签内链?网上有人说使用插件。 一开始我也是在网上看到别人推荐,使用的Simple Tags这个插件来完成tag内链的。 但是使用后,效果并不明显,很多打了标签却不内链。也不知道是不是我没有设置对。 最近又在网上找了一个方法,不利用插件来实现TAG内链,当然也是听到网友说使用插件会使网站...

wordpress如何实现tag标签内链?网上有人说使用插件。

一开始我也是在网上看到别人推荐,使用的Simple Tags这个插件来完成tag内链的。

但是使用后,效果并不明显,很多打了标签却不内链。也不知道是不是我没有设置对。

最近又在网上找了一个方法,不利用插件来实现TAG内链,当然也是听到网友说使用插件会使网站速度变慢,索性直接换成了代码方式实现。

将代码放在当前主题Functions.php 文件中。

也可通过后台 外观-主题编辑器 进入编辑。

将代码放在结尾 ?> 的前面

//自动TAG转内链
$match_num_from = 2; // 一个TAG标签出现几次才加链接
$match_num_to = 1; // 同一个标签加几次链接
add_filter('the_content','tag_link',1);
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){
global $match_num_from,$match_num_to;
$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, '$'),__('View all posts in %s'))."\"";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
$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;
}

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

有哪些知名的网站是用wordpress搭建的?

2023-2-16 15:47:39

wordpress教程

WordPress发布文章时间不准解决方法

2023-2-16 15:47:43

【腾讯云】11.11云上盛惠!云服务器首年1.8折起,买1年送3个月!
11.11云上盛惠!海量产品·轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。
查看更多相关信息>>
站长

(工作日 10:00 - 22:30 为您服务)

2026-05-13 02:25:31

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

猜你想问:

  • 购买的模板免费包安装吗?

  • 这个演示地址有吗?

  • 购买vip会员可以下载哪些模板?

您的留言我们已经收到,我们将会尽快跟您联系!
取消
立即选择任一渠道联系我们