有时候我们需要替换文章的某些文字为其它文字,比如给特定的文字加上链接,利用 SEO 和用户体验。

下边的代码即可实现这种功能:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/**

    *WordPress 替换文章的某些文字

    *http://www.11px.cn/

*/

function Bing_content_str_replace($text){

    $replace_words = array(

        //添加要替换的文本,格式:'要替换的' => '替换到的内容'

        '精准像素' => '<a href="http://www.11px.cn">精准像素</a>',

        '帝国cms' => '<a href="http://www.11px.cn">帝国cms</a>',

        'functions.php' => '<a href="http://www.11px.cn/wordpress/">functions.php</a>'

    );

    return str_replace( array_keys( $replace_words ), $replace_words, $text );

}

add_filter( 'the_content', 'Bing_content_str_replace' );

add_filter( 'the_excerpt', 'Bing_content_str_replace' );


标签:
点击这里复制本文地址

以上内容由qqso网站教程网整理呈现.
原文地址:WordPress教程替换文章的某些文字发布于2021-08-16 09:00:23。
请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!