方法一
wordpress查找替换文章指定字符串代码如下,加入到主题文件夹下的functions.php中即可:
add_filter('the_content','web589_replace'); function web589_replace($content){ $data=array( 'WP短代码'=>'<srong>WP短代码</strong>', /* 第一个引号的是原字符串,第二个引号是要被替换的字符串 */ '<a href="http://www.baidu.com">百度</a>'=>'<a href="http://www.google.com">google</a>', 'iphone4'=>'iphone4s' ); $content=str_replace(array_keys($data),$data,$content); return $content; }
方法二
//发布文章自动替换相关内容 function replace_text_huhexian($text){ $replace = array( //'关键词' => '将要替换的关键词' '例子' => '案列', 'wordpress' => 'wordpress主题', ); $text = str_replace(array_keys($replace), $replace, $text); return $text; } add_filter('the_content', 'replace_text_huhexian'); add_filter('the_excerpt', 'replace_text_huhexian');
方法三
function replace_text_wps($text){ $replace = array( // '关键词' => '替换的关键词' '这是老的内容' => '<a href="这是URL">这是新的带URL的内容</a>', '这是老的内容' => '这是新的内容' ); $text = str_replace(array_keys($replace), $replace, $text); return $text; } add_filter('the_content', 'replace_text_wps'); add_filter('the_excerpt', 'replace_text_wps');
方法四
刷新数据库方式,请在执行操作前,备份数据库!!!执行以下sql,批量替换wordpress中文章内容
UPDATE wp_posts SET post_content = replace(post_content,'这是老的内容','这是替换后的内容');