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
- 搬瓦工→DC6 机房 CN2 GIA 线路推出 10Gbps 带宽 VP全球[VPS测评]
- 优惠 80M 主机→全场 75 折优惠 可按小时计费 香港 CN2 GI香港VPS[主机]
- 倒着写的人字和马字是什么成语?全球[VPS测评]
- EUserv→德国独服 1Gbps 带宽 不限流量 免费 20 个 IP全球[VPS测评]
- 新年促销 糖果主机→无限空间 无限流量虚拟主机 6 折起 VPS 年付 虚拟空间(主机)
- ION→新加坡云服务器预售 PCCW 移动直连 电信走 CN2 $15 全球[VPS测评]
- 黑五 Hostdare→4 折优惠 CN2 GIA 超大硬盘 做站首选全球[VPS测评]
- 如何查看 VPS 的去程和回程全球[VPS测评]
- begin主题添加《英雄榜》每评论一次你就会自动排第一代码全球[VPS测评]
- RangCloud 推出山东联通NATVPS 256m内存1T流量100全球[VPS测评]
- 岚云 VPS 香港 CN2 GIA 线路 全程跑满 月付29.25元 还香港VPS[主机]
- 速云 全场八折 香港CN2GIA 美国高防CN2GIA 年付188起 月美国VPS[主机]
- PerfectQualityHosting提供乌克兰/拉脱维亚/摩尔多瓦全球[VPS测评]
- WordPress博客使用php页面代码实现外链go跳转的方法全球[VPS测评]
- 1分钟快速读懂云计算全球[VPS测评]
- 晚点独家丨拼多多9月上线跨境电商平台,首站美国美国VPS[主机]
- 实例 GIA0910629396 重启失败,原因:sudo: unabl全球[VPS测评]
- 香港云服务器对于建站有什么便捷的地方?香港VPS[主机]
- 云服务器和云服务器租用,云服务器做什么??全球[VPS测评]
- 美国信贷市场的痛苦将不亚于上世纪70年代的通胀时期美国VPS[主机]
- 云计算竞争日趋白热化云计算行业市场前景分析全球[VPS测评]
- 厘米云,江苏移动100G高防云服务器,4核4G内存20M带宽79元/月全球[VPS测评]
- 恒创科技:香港/美国云服务器2折起,年度特价低至13元/月,新购送京东E美国VPS[主机]
- 拼多多正筹建跨境电商平台:密集挖角SHEIN员工,零佣金招商入驻全球[VPS测评]
- 1分钟快速读懂云计算全球[VPS测评]
- 美云香港云服务器怎么样?1核1G香港云服务器价格99元/月香港VPS[主机]
- 八点云主机怎么样?香港2核4G5M带宽云主机,98元/月;国内4核8G云香港VPS[主机]
- 华纳云新春活动:香港CN2云服务器4核4G5M仅3476元/3年;美国云美国VPS[主机]
- ucloud香港服务器怎么样?听听这些网友们怎么说的香港VPS[主机]
- 奇异互动,2核4G香港云服务器仅655元/年,注册领550元新手大礼包香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-134938.html











