如果你想替换某些文章或评论中的文字,比如敏感词和过期内容,或者给某些关键字添加链接,比如推广链接等,那么你将下面的代码添加到主题的 functions.php 即可:
/**
* 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'); //评论
请根据自己的实际需求,修改要替换的内容,以及要应用的钩子。
如果你要永久替换某些文字,建议你试试 WordPress批量搜索和替换插件:Search & Replace
来源:
https://www.wpdaxue.com/replace-text-of-content-or-comment.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32722.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57