加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > 正文

WordPress自动为文章标签添加该标签的链接

发布时间:2020-05-25 00:26:52 所属栏目:CMS系统 来源:互联网
导读:站长朋友们在使用WordPress建站的过程中,当网站中的文章(post)和标签(tag)越来越多时,很多时候就希望给文章中出现的标签(tag)自动添加上该标签的链接。这篇WordPress教程里,我们就向大家介绍一下具体的方法。

站长朋友们在使用的过程中,当网站中的文章(post)和标签(tag)越来越多时,很多时候就希望给文章中出现的标签(tag)自动添加上该标签的链接。这篇WordPress教程里,我们就向大家介绍一下具体的方法。

切换到主题目录,打开functions.php文件,加入以下代码:

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(); //$posttags = get_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 = ""; $limit = rand($match_num_from,$match_num_to); //不链接的代码 $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)(]*>)|U'.$case,'$1$2%&&&&&%$4$5',$content); $content = preg_replace( '|(WordPress自动为文章标签添加该标签的链接)|U'.$case,$content); $cleankeyword = preg_quote($cleankeyword,'''); $regEx = ''(?!((<.*?)|(]*?)>)|([^>]*?))'s' . $case; $content = preg_replace($regEx,$url,$content,$limit); $content = str_replace( '%&&&&&%',stripslashes($ex_word),$content); } } return $content; })|U')|([^>]*?))'s'

注意:该代码会为这篇文章的标签自动添加链接,但是如果您想给网站中设置的所有标签自动添加链接,请将上述代码中的第 13 行修改为:

另外,$match_num_from 和 $match_num_to 这两个参数的设置不够准确,具体请站长朋友们自己进行测试。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读