2012-02-25 43 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;