我们每一个站点都有一些主关键词,比如本站的boke112百科、JianYue、WordPress等等,那么如何实现发布的文章中包含有这些主关键词的,就自动为它们添加上链接?
将以下代码添加到当前主题的functions.php文件中并保存更新文件即可。
//WordPress站点为主关键词自动添加链接
function replace_main_keyword($text){
$replace = array(
'boke112百科' => '<a href="https://boke112.com/">boke112百科</a>',
'JianYue' => '<a href="https://boke112.com/post/7613.html">JianYue</a>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'replace_main_keyword');
记得将上述代码中的第3~4行代码修改为自己站点的主关键词,也可以添加更多的主关键词和链接。