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
- CoalCloud→648 元 年 1GB 内存 20GB SSD 空间虚拟空间(主机)
- 优惠 糖果主机→美国云服务器上线 年付 348 元起 另有中美极速专线套美国VPS[主机]
- 按时计费 $2.8 月 512M 内存 10G 硬盘 1T 流量@1Gb全球[VPS测评]
- NameSilo→.xyz .top 等域名 首年 0.99 美金 一次全球[VPS测评]
- php 多个数组根据指定键值分组方法全球[VPS测评]
- gcorelabs→€4.49 月 新加坡 VPS 50Mbps 带宽 全球[VPS测评]
- 推荐 iON→KT 旗下品牌 CN2 线路 洛杉矶机房 全场限时 5 折全球[VPS测评]
- losangelesvps→低至$25 年的 KVM 系列 VPS 洛杉全球[VPS测评]
- uovz 青岛 200G 高防独服上线,青岛联通高防,青岛 BGP 高防全球[VPS测评]
- 腾讯云秒杀→个人云服务器年付 99 元起 香港云服务器年付 249 元起香港VPS[主机]
- 便宜 $15 年 2 核 CPU 2G 内存 50G SSD 5T 流量全球[VPS测评]
- Windows 系统 远程桌面 漏洞公告 及如何安装安全补丁WINDOWS
- 腾讯内部单边拥塞算法 BBR-TCPA 一键脚本安装及速度测评全球[VPS测评]
- 优惠 Hosteons→Resource Pool 系列产品 7 折优惠全球[VPS测评]
- Buyvm→$2 月 512MB 内存 10GB SSD 空间 不限流量虚拟空间(主机)
- 大容量便宜 VPS 汇总推荐→5 美元 月左右 空间 500GB 以上虚拟空间(主机)
- 疯狂猜成语 图猜成语四个芋头一个是坏了一个是一半的是什么成语?全球[VPS测评]
- 引力主机双12九折优惠安徽联通NATVPS1G内存4T大流量200M大带全球[VPS测评]
- WordPress博客使用php页面代码实现外链go跳转的方法全球[VPS测评]
- WordPress基于WPJAM basic开发的主题:Sweet全球[VPS测评]
- LiteServer → 黑五 网络星期一 荷兰 4折优惠 NVME V全球[VPS测评]
- Unicom lv-9€ 月 拉脱维亚 1C2G10G硬盘 不限流量 支全球[VPS测评]
- [信息]CloudCone 1811促销全球[VPS测评]
- 什么是云计算?全球[VPS测评]
- 一篇漫画,看懂云计算全球[VPS测评]
- 企鹅小屋:香港ntt vps,大带宽直连,1核/1G/2T流量/半年15香港VPS[主机]
- 亿恩科技香港云服务器 2核2G服务器特价700元/年香港VPS[主机]
- 傲游主机:香港cn2高防云主机,159元/月/2核/2GB/40GB/2香港VPS[主机]
- 关键路径、资金情况、政策关键数据中心的可持续发展路径在哪里?全球[VPS测评]
- 弘速科技:香港安畅CN2 GIA/弹性云服务器10元/月,2核1GB/2香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-144622.html











