打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
wordpress文章自动添加标签链接 | wordpress教程网
userphoto

2015.08.31

关注

我们在编写文章时,经常需要添加一些标签关键词的链接,这样不仅可以优化我们的内链,对用户来说也可以参照相关的文章,如果对文章的关键字进行手动添加链接,那样对我们来说太麻烦了,而且在标签关键词很多的情况下我们是记不住的,今天就向大家介绍如何让我们的wordpress文章自动添加标签关键词的链接:
打开我们主题的functions.php文件添加如下代码:

// 文章添加关键词链接//连接数量$match_num_from = 1;  //一篇文章中同一个关键字少于多少次锚文本链接(这个直接填1就好了)$match_num_to = 1; //一篇文章中同一个关键字最多出现多少次描文本(建议不超过2次)//连接到WordPress的模块add_filter('the_content','tag_link',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 = "<span class=\"tag-span\"><a class=\"tag\" href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\"";$url .= ' target="_blank"';$url .= ">".addcslashes($cleankeyword, '$')."</a></span>";$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;}

OK,当我们的文章出现过我们添加的标签关键词时,就会自动添加链接,你可以查看本站实例,如下图:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
郑景承:wordpress采集页简单改造调用代码和说明,提升收录量
15个实用的PHP正则表达式
4 Tips about how to optimize wordpress for seo | Sven's Blog
15个超实用的php正则表达式
php截取html字符串自动补全html标签
Php
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服