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
- CoalCloud→268 元 月 2 核 2GB 内存 20GB SS虚拟空间(主机)
- php 使用 md5 函数生成 16 位 md5 值返回乱码的原因以及解全球[VPS测评]
- STSDUST→$15 年 512MB 内存 8GB SSD 空间 1T虚拟空间(主机)
- Xmas sugarhosts→6 折促销美国 cn2 线路的虚拟主机美国VPS[主机]
- php 数组求和函数 array_sum()全球[VPS测评]
- 搬瓦工最便宜的 cn2vps 补货,10Gbps 大带宽服务器,46.8全球[VPS测评]
- KVMLA 新加坡&日本独立服务器 75 折 首月再返 300 元 充 独立服务器[U]
- 年付慎重-Tenvm→香港 NAT 1 核 256M 内存 5G SSD香港VPS[主机]
- 疯狂猜成语 图猜成语一个老人说了很多话旁边一个人在叹气是什么成语?全球[VPS测评]
- HostUS→OpenVZ 年付 16 美元起 KVM 月付 5.56 全球[VPS测评]
- [JSAPI] config: invalid url domain 微全球[VPS测评]
- 疯狂猜成语 图猜成语一个人在搬一块很大的石头是什么成语?全球[VPS测评]
- ByteDynasty→11 元 月 512MB 内存 15GB SSD虚拟空间(主机)
- 如何在百度搜索结果中显示你的WordPress站点Logo全球[VPS测评]
- WordPress免插件代码实现Gravatar头像缓存全球[VPS测评]
- VPS推荐:io/zoom美国VPS主机美国VPS[主机]
- EtherNetservers促销套餐 1G内存/30G硬盘/2个IP 全球[VPS测评]
- CloudCone便宜 KVM VPS,1G内存/50GB容量/1Gbp全球[VPS测评]
- HTML5绿色有机食品电商网站模板 - Organtio全球[VPS测评]
- 如何根据业务需求选择合适的云服务器配置?全球[VPS测评]
- 原创解析:云计算含义及定义全球[VPS测评]
- 什么是云服务器?云服务器怎么登陆?全球[VPS测评]
- 宏海数据9.9云服务器:香港VPS CN2线路1核1G 9.9/首月香港VPS[主机]
- 印象云vps,香港安畅gia低至22元起,1核1G内存,小带宽无限流量,香港VPS[主机]
- 40火伞高压电爬坡更让企业用电于民全球[VPS测评]
- UCloud海外云服务器促销:全场1折起,香港cn2vps最低年付150香港VPS[主机]
- 7月份,经济复苏边际趋缓,央行降息幅度先稳后升全球[VPS测评]
- Hostkvm:香港/韩国CN2云服务器 大带宽VPS 2核4G,$7.韩国VPS[主机]
- wordpress 上传附件报错 “抱歉,出于安全的考虑,不支持此文件类全球[VPS测评]
- HostKvm:洛杉矶云主机/香港云主机八折优惠,大带宽,月付5.2美元香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-149794.html











