WordPress SEO优化 如何自动为文章添加标签链接增加站点内链
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

关于WordPress SEO优化蜗牛789也介绍过一点皮毛,因为蜗牛也是小白,不会怎么做SEO优化,都是学习前辈的WordPress教程,大多都是按部就班。在此文章中蜗牛将为大家分享一个无需任何插件,纯代码实现“自动为WordPress文章添加标签链接从而实现增加站点内链”。说是分享,其实是为了记录以下,方便自己以后查找,因为此方法百度一搜,比比皆是。话说回来虽然百度能搜到有一大把,但是有些细节问题还是要注意,并不是都是非常完美的。具体方法如下:
如何自动为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 h=\\\"$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);或
//自动TAG转内链$match_num_from = 1; // 一个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文件后,当我们发布、更新、保存文章时,会自动检测文章中是否有标签关键词,如果有就会自动添加这些标签的链接。虽然是自动的,但我们还需注意以下两点:
1、上面代码中$match_num_to = 1; //一篇文章中同一个标签最多自动链接几次,这个建议最多只设置3次。
2、代码中的$posttags = get_tags();表示自动为文章添加全站所有标签链接,我们还可以改成只为此文章设置的标签添加链接,只需将“$posttags = get_tags();”改成“$posttags = get_the_tags();”即可。
一般建议使用蜗牛推荐代码,无需做任何修改。
[WordPress SEO优化]历史优惠活动内容
猜你可能想看的VPS
- 疯狂猜成语 图猜成语狗的身体老虎的头是什么成语?全球[VPS测评]
- 劳动节促销 Hosteons→美国不限流量 VPS 八折优惠 OVZ 架美国VPS[主机]
- 初春优惠 糖果主机→香港云服务器¥27.75 月起 虚拟主机新购最低可享香港VPS[主机]
- Onevps→日本机房不限流量 VPS 月付 5 美元起 可看奈飞 Hy日本VPS[主机]
- 做站首选 腾讯云服务器秒杀活动→上海机房 2G 内存 VPS 年付 19全球[VPS测评]
- 腾讯云星星海 SA2 云服务器→2C4G6M 三年 1735 元全球[VPS测评]
- AkkoCloud→120 元 月 1GB 内存 10GB SSD 空间虚拟空间(主机)
- hosts 文件位置全球[VPS测评]
- 超值 腾讯云助力复工→香港 1 核 1G 3M 带宽机器年付 299 元香港VPS[主机]
- 补货 CloudCone→512M 内存套餐 20 美元每年的套餐又补货全球[VPS测评]
- 不限流量 OneVPS→首月 5 折 终身 9 折 多国机房 亚洲移动线全球[VPS测评]
- 阿里云香港 新加坡 30Mbps 轻量应用服务器 1 核 1G 24 元香港VPS[主机]
- centOS7系统下如何修改服务器SSH端口默认的22。全球[VPS测评]
- 私有文档、源码、软件目录列表程序:Zdir全球[VPS测评]
- 云计算的大门依旧没向雷军敞开全球[VPS测评]
- 个人如何做跨境电商?全球[VPS测评]
- 租用美国服务器有哪些安全方案美国VPS[主机]
- 恒创科技怎么样?香港美国云服务器/独服/高防全场2.5折起美国VPS[主机]
- 八成企业看好出海投资 防止“溺水”需重视这一点全球[VPS测评]
- kvmla,新加坡VPS、香港VPS、日本VPS,2核2G云主机仅425日本VPS[主机]
- anyhk香港HKT商宽NAT VPS,1Gbps无限流量,终身8折¥3香港VPS[主机]
- 美国信贷市场的痛苦将不亚于上世纪70年代的通胀时期美国VPS[主机]
- 搬瓦工VPS:高端线路助力企业运营,10Gbps美国 cn2 gia,1日本VPS[主机]
- 云计算的三种服务模式,SaaS和PaaS的区别在哪里?全球[VPS测评]
- 酷锐云:元旦钜惠_美国CN2VPS低至20元/月_1核2G香港云主机低至美国VPS[主机]
- TabbyCloud:1核/1GB/20GB/2TB流量/50Mbps/香港VPS[主机]
- 漂亮的Css3响应侧边栏菜单,动画风格侧边栏导航菜单样式全球[VPS测评]
- Mineserver:香港CMI大带宽云服务器38元/月起(300M不限香港VPS[主机]
- 月神科技:香港沙田CN2 GIA VPS,1核/1G/10M/40GSS香港VPS[主机]
- 微基主机:1核1G香港云服务器,100Mbps/香港CN2直连/KVM/香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-149794.html











