2012-02-25 40 views
5

我想剝離我的博客文章中的[gallery]簡碼。我找到的唯一解決方案是添加到我的函數中的過濾器。WordPress的從郵件中剝離短代碼

function remove_gallery($content) { 
    if (is_single()) { 
    $content = strip_shortcodes($content); 
    } 
    return $content; 
} 
add_filter('the_content', 'remove_gallery'); 

它刪除所有的短代碼,包括[caption],我需要的圖像。我如何指定一個簡碼來排除或包含?

+0

你能提供一個或兩個簡碼的例子嗎? – 2012-02-25 02:28:04

回答

13

僅去除畫廊簡碼,註冊,返回一個空字符串回調函數:

add_shortcode('gallery', '__return_false'); 

但是,這將只與回調的工作。要做到這一點靜態,你可以臨時更改的WordPress的全局狀態愚弄它:

/** 
* @param string $code name of the shortcode 
* @param string $content 
* @return string content with shortcode striped 
*/ 
function strip_shortcode($code, $content) 
{ 
    global $shortcode_tags; 

    $stack = $shortcode_tags; 
    $shortcode_tags = array($code => 1); 

    $content = strip_shortcodes($content); 

    $shortcode_tags = $stack; 
    return $content; 
} 

用法:

$content = strip_shortcode('gallery', $content); 
+1

不錯,但你需要返回其他的短碼,不要忘記之後的echo do_shortcode($ content)。 – Benn 2016-04-07 16:28:58

-1

如果你想獲得只有內容,不包括任何簡碼,你可以試試那

global $post; 
$postContentStr = apply_filters('the_content', strip_shortcodes($post->post_content)); 
echo $postContentStr;