温馨提示:本文更新于2026-05-14,某些文章具有时效性,若有错误或已失效,请在下方留言!
为提升SEO效果与站内内容关联性,可在子比主题中实现 文章关键词自动添加内链 功能。当文章正文出现指定关键词(如标签、核心术语或自定义词汇)时,系统会自动将其链接到对应的标签页、分类页或自定义URL。本教程提供纯代码方案,无需额外插件,通过在子比主题的functions.php文件中添加轻量级函数,即可智能匹配并生成内链,兼顾性能与效果。
WordPress文章关键词自动内链,在functions.php当前目录下新建一个 func.php 的文件,然后在最顶部写上 <?php ,再写入以下php代码
),__('更多关于 %s的文章 _ 好软猫 _www.haoruanmao.com ')).""";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '
[/hidecontent]
)."</a>";
$limit = rand($match_num_from,$match_num_to);
$content = preg_replace( '|(<a[^>]+>)(.*)<pre.*?>('.$ex_word.')(.*)</pre>(</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;
}
add_filter('the_content','tag_link',1);
//WordPress文章关键词自动内链 _ 好软猫 _www.haoruanmao.com
- 最新
- 最热
只看作者