2019-01-21
分类:未分类
阅读(671) 评论(0)
如果你想替换某些文章或评论中的文字,比如敏感词和过期内容,或者给某些关键字添加链接,比如推广链接等,那么你将下面的代码添加到主题的 functions.php 即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/**
* WordPress 快速替换文章/评论的某些文字内容
* https://www.wpdaxue.com/replace-text-of-content-or-comment.html
*/
function wpdaxue_replace_text($text){
$replace = array(
// '原始文字' => '替换为这些'
'WP大学' => 'WordPress大学',
'阿里云' => '<a href="https://www.wpdaxue.com/go/aliyun">阿里云</a>',
'倡萌' => '<a href="http://www.cmhello.com/">倡萌</a>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'wpdaxue_replace_text'); //正文
add_filter('the_excerpt', 'wpdaxue_replace_text'); //摘要
add_filter('comment_text', 'wpdaxue_replace_text'); //评论
|
/** * WordPress 快速替换文章/评论的某些文字内容 * https://www.wpdaxue.com/replace-text-of-content-or-comment.html */ function wpdaxue_replace_text($text){ $replace = array( // ‘原始文字’ => ‘替换为这些’ ‘WP大学’ => ‘WordPress大学’, ‘阿里云’ => ‘<a href=”https://www.wpdaxue.com/go/aliyun”>阿里云</a>’, ‘倡萌’ => ‘<a href=”http://www.cmhello.com/”>倡萌</a>’ ); $text = str_replace(array_keys($replace), $replace, $text); return $text; } add_filter(‘the_content’, ‘wpdaxue_replace_text’); //正文 add_filter(‘the_excerpt’, ‘wpdaxue_replace_text’); //摘要 add_filter(‘comment_text’, ‘wpdaxue_replace_text’); //评论
请根据自己的实际需求,修改要替换的内容,以及要应用的钩子。
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:
IDC资讯中心 »
WordPress 快速替换文章/评论的某些文字内容