WordPress 自动内链关键词

Gavin Wu 2022年04月08日 7:04 Wordpress 862 Views

有时,我们想有利于SEO的情况下,将博客内文章所有有关博客的关键词都进行内链,如果自己一个一个手动修改的话比较麻烦,可以通过以下代码批量自动内链关键词。

1
2
3
4
5
6
7
8
9
10
11
/* 文章指定关键词添加内链开始 */
function replace_text_wps($text){
    $replace = array(
            '微言心语' => '<a href="https://wuean.com" target="_blank" title="微言心语的博客" rel="noopener">微言心语</a>',
            'GavinWu' => '<a href="https://wuean.com" target="_blank" title="GavinWu发布的文章" rel="noopener">GavinWu</a>',
            'Wordpress' => '<a href="https://wuean.com/category/wordpress" target="_blank" title="Wordpress相关内容" rel="noopener">Wordpress</a>',
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}
add_filter( 'the_content', 'replace_text_wps');

演示:
WordPress 自动内链关键词|微言心语
特别要注意这种方法对于英文比较呆板,能精确到大小写。什么意思呢,就像上面代码中的Wordpress,如果的文章中wordpress的W不是大写他是不会链接的。

文章评论1 条

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Top