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
- CombCloud→58 元 月 1GB 内存 20GB SSD 硬盘 香港VPS[主机]
- 五月便宜 数脉科技→香港独立服务器 月付 272 元 随机配置最低 E3独立服务器[U]
- STSDUST→$30 年 384MB 内存 5GB 空间 1TB 流量虚拟空间(主机)
- 支付宝开发扫描生成的付款二维码提示→码已失效怎么办?全球[VPS测评]
- JQuery.validate 验证 hidden 隐藏域的方法全球[VPS测评]
- GeorgeDatacenter→$15 年 1GB 内存 100GB 虚拟空间(主机)
- Element ui 中 upload 组件一次请求批量上传全球[VPS测评]
- hLink→€20 年 1GB 内存 15GB SSD 空间 不限流量 虚拟空间(主机)
- 如何用PHPStudy搭建本地PHP测试环境及安装Wordpress全球[VPS测评]
- WordPress 网站自定义广告位占位/出租代码全球[VPS测评]
- Ucloud/阿里云/腾讯云安装宝塔面板后无法访问,如何放行端口全球[VPS测评]
- Virmach → $1.5 月付 512M 内存 10G SSD硬盘 全球[VPS测评]
- PacificRack → 512M 25G SSD 500G $12.全球[VPS测评]
- 宝塔面板在香港服务器安装启动不了nginx怎么办?香港VPS[主机]
- 拼多多拟建跨境电商平台:挖角SHEIN员工,0佣金招商入驻全球[VPS测评]
- CIO和其他IT领导者充分利用边缘计算增强业务的4个关键全球[VPS测评]
- 极光kvm怎么样,便宜大带宽香港cmi vps/美国gia vps低至1美国VPS[主机]
- 极光kvm怎么样?香港大带宽VPS促销1核1G内存19元/月起,5G防御香港VPS[主机]
- 磐逸云怎么样?香港沙田cn2vps 带宽5M年付128元香港VPS[主机]
- 六六云:香港BGP云服务器/80M大带宽,月付45元起,5折优惠码可用香港VPS[主机]
- centos7如何查看firewall防火墙状态:开启/关闭/重启防火墙全球[VPS测评]
- 二三互联年末回馈:香港/美国云服务器新购特价促销,优惠码可打76折美国VPS[主机]
- 雪花云怎么样?雪花云香港云服务器1核1G2M仅19元/月香港VPS[主机]
- JGKVM活动钜惠:美国cn2,香港cmi,大带宽vps,三网直连,10美国VPS[主机]
- 手帕云,便宜小带宽香港CN2 VPS,集群10G防御,双向CN2直连,仅香港VPS[主机]
- 恒创科技:开年采购季_香港CN2云服务器/美国CN2线路,低至299元/美国VPS[主机]
- 香港服务器租用哪里有?便宜的香港服务器租用香港VPS[主机]
- tmhhost:香港CN2/NNT,美国CN2 GIA VPS,韩国CN韩国VPS[主机]
- 云计算的三种服务模式,SaaS和PaaS的区别在哪里?全球[VPS测评]
- 触摸云:香港GT ④区适合建站vps,折后最低套餐29.75/月香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-128577.html











