2013-02-25 63 views
1

我此行的主題顯示摘錄代碼:刪除簡碼(有代碼)

<p class="desc"><?php echo mb_strimwidth(strip_tags(get_the_content('')), 0, 220, '...'); ?></p> 

我如何把這個代碼去掉從摘錄的簡碼?

$text = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', $text); 

我剛剛開始削減PHP,所以我只需要一點點的幫助。

謝謝!

回答

3

而不是重新發明輪子,我建議你使用核心WordPress功能strip_shortcodes()

<p class="desc"><?php echo mb_strimwidth(strip_shortcodes(strip_tags(get_the_content(''))), 0, 220, '...'); ?></p> 
0

不要在這種情況下使用REGEX!

正則表達式將從摘錄刪除自己的筆記,例如:
Hello, on May 27 [1995] , blabla

所以,最好使用內置函數strip_shortcodes,檢測註冊的簡併刪除它們:

add_filter('the_excerpt','myRemoveFunc'); function myRemoveFunc(){ 
    return mb_strimwidth(strip_shortcodes(get_the_content()), 0, 220, '...'); 
}