批量替換是網站運行中經常遇到的操作,例如前面我們介紹過的《織夢dedecms批量替換文章標題內容》、《織夢DEDECMS批量修改域名下所有文章內的圖片路徑》等等,都是需要進行批量操作,WordPress網站很多時候也是有這樣的操作要求的,有時候我們需要替換文章的某些文字為其它文字,比如給特定的文字加上鏈接等,該如何實現呢?
下邊361源碼就給出相應的實現代碼 即可實現這種功能:
/** *WordPress 替換文章的某些文字 *http://www.49028c.com/*/function Bing_content_str_replace($text){ $replace_words = array( //添加要替換的文本,格式:'要替換的' => '替換到的內容' 'adminbuy' => '<a href="http://www.49028c.com">vevb.com</a>', '魔客吧' => '<a href="http://www.49028c.com">361源碼網</a>', 'functions.php' => '<a href="http://www.49028c.com/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' );
這樣就可以了。
新聞熱點
疑難解答
圖片精選