创建WP_Keyword_Link.php文件,将下列代码复制进去,上传入plugin目录,后台启用即可
<?php /* Plugin Name: 关键词自动内链 Description: 文章内的关键词标签自动添加内链。 Version: 1.0 Author: artcg.design Author URI: https://artcg.design */ //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 = ""; $limit = rand($match_num_from,$match_num_to); //不链接代码 $content = preg_replace( '|(]+>)(.*)('.$ex_word.')(.*)<\/pre>( ]*>)|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( '%&&&&&%', stripslashes($ex_word), $content); } } return $content; } add_filter('the_content','tag_link',1);
原文链接:https://www.artcg.design/1447.html,转载请注明出处。
评论0