干净的代码实现WordPress文章中标签文本的自动内部链接
将下面的代码复制到当前主题的functions.php文件的最后一个代码中? >Before: /* 开始自动添加文章内部链接*/
$match_num_from = 1; //如果文章相同标签次数少于几次,则不会自动链接
$match_num_to = 1; //一篇文章中同一标签最多可自动链接次数
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0 ;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
-函数 tag_link($content){
来自源的全局 $hit_number, $hit_number;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach($posttags $tag) {
https://www.cainiaoxueyuan.com/youhua/$link get_tag_link( $tag->term_id);
$keyword = $tag->name;
$cleankeyword = 破折号($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( '|() |U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?! ((]*?))\'s' 。 $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%',lines($ex_word), $content);
}
}
return $content;
}
add_filter('the_content','tag_link',1);
/* 自动为文章添加内部链接到标签里面 /
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。