把下面代码展开复制粘贴到主题目录下的functions.php文件里面就可以了。其中可以修改match_num_from和match_num_to的值,建议把值设置大一点,这样文章内所有同个关键词都能内部链接。
- /*Wordpress文章关键词自动添加内链链接代码
- */
- //连接数量
- $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;
- }
以上代码在php7.4正常使用,在php8.0以上,会出错,优化后代码如下:
- // 连接数量配置
- $match_num_from = 1; // 一个关键词最少替换次数
- $match_num_to = 1; // 一个关键词最多替换次数
- // 挂载到WordPress内容过滤器
- add_filter('the_content', 'tag_link', 1);
- // 按标签名称长度排序(长关键词优先)
- function tag_sort($a, $b) {
- return strlen($b->name) <=> strlen($a->name); // PHP 7+太空船操作符
- }
- // 核心关键词替换逻辑
- function tag_link($content) {
- global $match_num_from, $match_num_to;
- // 初始化可能未定义的变量
- $ex_word = '';
- $case = 'i'; // 默认不区分大小写
- $posttags = get_the_tags();
- if ($posttags) {
- usort($posttags, "tag_sort");
- foreach ($posttags as $tag) {
- // 安全获取标签属性
- $tag_name = isset($tag->name) ? $tag->name : '';
- if (emptyempty($tag_name)) continue;
- $link = get_tag_link($tag->term_id);
- $cleankeyword = stripslashes($tag_name);
- $escaped_keyword = preg_quote($cleankeyword, '#');
- // 构建链接HTML
- $url = '<a href="'.$link.'" title="查看所有关于 '.esc_attr($cleankeyword).'的文章" target="_blank">'.esc_html($clealkeyword).'</a>';
- // 生成随机替换次数
- $limit = rand($match_num_from, $match_num_to);
- // === 修复1:使用#作为正则分隔符避免冲突 ===
- // 排除已有链接内的关键词
- $content = preg_replace(
- '#(<a[^>]+>)(.*?)('.$escaped_keyword.')(.*?)(</a[^>]*>)#U'.$case,
- '$1$2%&&&&&%$4$5',
- $content
- );
- // 排除图片标签内的关键词
- $content = preg_replace(
- '#(<img[^>]*?)(.*?)('.$escaped_keyword.')(.*?)(>)#U'.$case,
- '$1$2%&&&&&%$4$5',
- $content
- );
- // === 修复2:重构正则表达式结构 ===
- $regEx = '#(?!<.*?)(?<!["\']/?>)'. $escaped_keyword . '(?!([^<]*?>)|([^>]*?</a>))#s' . $case;
- // 执行关键词替换
- $content = preg_replace($regEx, $url, $content, $limit);
- // 恢复被排除的关键词
- $content = str_replace('%&&&&&%', $cleankeyword, $content);
- }
- }
- return $content;
- }