WordPress自动设置标签Tag自动内链无插件实现方法
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

之前蜗牛博客一直有使用WP keyword Link Plugin插件,但是发现这个插件已经好久没有更新,好像目前在平台中已经找不到。所以准备替换掉这个插件。类似的WordPress插件还是有很多的,比如Keywords to Links Converter、Auto Tag Links等都可以实现。
本着少用插件,能不用插件就不用的策略,所以准备还是无插件实现。
//自动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 h=\\\\\\”$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;
}
将代码添加到当前主题Functions.php文件中。如果有冲突出现问题,我们需要检查是不是不兼容。检查不出来的话,我们只能使用插件实现。
猜你可能想看的VPS
- 黑五 HostDare 促销→CN2 GT 线路 75 折优惠 CN2 全球[VPS测评]
- 充值活动 ¥60 月 2G 内存 35G SSD 7Mbps 不限量 新全球[VPS测评]
- SiliconHouse→$45 月 E3-1230v2 32GB 内存全球[VPS测评]
- 增配通知 Vultr→不加价为 VPS 套餐增加 30%-220%的 S虚拟空间(主机)
- 便宜香港空间 老薛主机→影视 淘客建站扶持计划 买一年送一年 最多可送三虚拟空间(主机)
- Rainbow Cloud→$30 月 E3-12XX 8G 内存 1T日本VPS[主机]
- Data Ideas→$1.5 月 1GB 内存 10GB SSD 空间虚拟空间(主机)
- CentOS7 安装 Docker 教程全球[VPS测评]
- 无版权服务器 荷兰服务器 VPS underhost全球[VPS测评]
- STSDUST→$15 年 512MB 内存 8GB SSD 空间 1T虚拟空间(主机)
- MoeCloud→25 元 月特价机补货 CN2 GIA 线路 可以看奈全球[VPS测评]
- 特价服务器 Raksmart→美国不限流量服务器低至 399 元 月 C美国VPS[主机]
- edgenat新上cera三网cuvip VPS云服务器,全场六折,1G全球[VPS测评]
- WordPress网站PHP纯代码生成文章海报图片实现分享全球[VPS测评]
- WordPress网站运营必备的浏览器插件5118站长工具箱全球[VPS测评]
- WordPress性能优化初级方案——提升网站速度飞起来全球[VPS测评]
- Weasel Cloud → 2.2$ 月 1C1G20G硬盘 俄罗斯 全球[VPS测评]
- 加拿大以“国安”为由禁止华为、中兴参与5G建设全球[VPS测评]
- 二三互联,香港cn2云服务器5折+85折双重优惠,稳定不限流量,1核1G香港VPS[主机]
- 六六云,2核2G香港云服务器/1000GB流量/50Mbps端口/KVM香港VPS[主机]
- 磐逸云:全场8折优惠,1核1G香港套餐月付24元,日本NNT线路套餐,月日本VPS[主机]
- 阿里行癫:如何理解当下的云计算全球[VPS测评]
- CMIVPS:香港vps/2核/2GB/20GB空间/不限流量/KVM/虚拟空间(主机)
- 阿里云免备案服务器怎么样?阿里云香港服务器价格多少钱一年?香港VPS[主机]
- 云计算的大门依旧没向雷军敞开全球[VPS测评]
- 亿速云香港服务器助力企业出海 有哪些具体的特点和优势?香港VPS[主机]
- SugarHosts糖果主机双十一大促销 虚拟主机最高6折优惠全球[VPS测评]
- 傲游主机:香港cn2高防云主机,159元/月/2核/2GB/40GB/2香港VPS[主机]
- 常见的CN2,GIA,CIA 各个网络线路解释全球[VPS测评]
- 弘速云:4月特惠,美国GIA/香港CN2云主机首月5折,首月12.5元起美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-144622.html











