如果你想替換某些文章或迴響中的文字,比如敏感詞和過期內容,或者給某些關鍵字添加鏈接,比如推廣鏈接等,那麼你將下面的代碼添加到主題的 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’); //迴響
請根據自己的實際需求,修改要替換的內容,以及要應用的鉤子。
如果你要永久替換某些文字,建議你試試 WordPress批量搜索和替換外掛:Search & Replace