笨猫博客

  • 🍟首页
  • 🍘目录
    • 🥝VPS教程
    • 🍾猫玩技术
    • 🍹干货分享
    • 🍏软件分享
    • 🍩一只猫
  • 🍋工具
    • 🌽IP路由追踪
    • 🍐域名Whois查询
    • 🥘域名被墙查询
    • 🍧IP正常检测
    • 🔥IP端口检测
    • 🍆短网址
    • 🐟VIP音乐播放
    • 🍯KMS激活
  • 🍓链接
  • 🍪联系
  • 🍱登录
    • 🥦登录
    • 🍒注册
关注互联网,生活,音乐,乐此不疲的一只笨猫
  1. 首页
  2. 猫玩技术
  3. 正文

WordPress文章关键词自动添加链接

2020-01-12 5548点热度 0人点赞 0条评论

文章页面的关键词自动添加内链,有利于SEO,方便读者查看所有相关关键词的所有文章。
建议最多替换2个重复的词!避免过度SEO。
在主题的functions.php文件中添加以下代码:

/*
*自动为文章内的标签添加内链开始
*/
//连接数量
	$match_num_from = 1; //一个关键字少于多少不替换
	$match_num_to = 1; //一个关键字最多替换次数
	//连接到WordPress的模块
	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 href=\"$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[^>]+>)(.*)('.$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;
	}
标签: wordpress 超链接
最后更新:2020-01-12

笨猫

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

最新 热点 随机
最新 热点 随机
Windows/Office 激活工具HEU KMS Activator v63.0.0 WinRAR7.13中文商业版以及授权文件 开源免费的2FA工具推荐(华为手机避坑Microsoft Authenticator 微软验证器) 情侣飞行棋:比吵架有趣,比看剧更亲密 Windows11美化方案教程 简洁、纯HTML导航项目:XG-NAV(开源)
Windows/Office 激活工具HEU KMS Activator v63.0.0Windows11美化方案教程情侣飞行棋:比吵架有趣,比看剧更亲密开源免费的2FA工具推荐(华为手机避坑Microsoft Authenticator 微软验证器)WinRAR7.13中文商业版以及授权文件
我不是罗密欧!!! AI画图:stable-diffusion_webui on colab使用教程 CN2线路、商家整理,以及测试 秘术师装备属性一览 PAYPAL香港账户申请注册教程 WordPress:博客主题子比主题最新版本V2.0正式版[3.20]
最近评论
天天下载Ttzip 发布于 2 天前(09月15日) 很棒的网站!感谢分享,谢谢站长!!@天天下载Ttzip
诗音 发布于 3 周前(08月29日) 时间改到2099年在暂停更新也可以
vip券网 发布于 2 个月前(08月01日) 写的很详细,赞 :razz: :evil:
萧瑟 发布于 2 个月前(07月14日) 这个我也用了好久,不知道啥时候能上IP证书
梵蒂冈 发布于 2 个月前(07月08日) 士大夫
标签
elliptictrue100.10.02
BLOG ChatGPT Chrome CloudFlare DirectAdmin github Godaddy google kloxo Linux OneDrive OneIndex PHP QQ shadowsocks SNS ssh Typecho VPS web2.0 whmcs Windows wordpress 一键脚本 下载 免费 博客 图文教程 图标 图片 域名 头像 奥运 宝塔面板 插件 教程 日记 有趣 浏览器 游戏 猫 生活 电影 百度 网站 视频 设计 软件 阿里云盘 音乐
好友
  • glzjin's blog glzjin's blog
  • ZAERA博客
  • 冰沫记
  • 奇它博客
  • 猫腻‘s Blog
  • 猫饭
  • 肥宅之家
  • 萌博
  • 野路子程序员

COPYRIGHT © 2022 笨猫博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang