2012-02-25 21 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

短コードか2つの例を挙げることができますか? –

答えて

13

は、唯一のギャラリーショートコードを削除し、空の文字列を返すコールバック関数登録するには:

add_shortcode('gallery', '__return_false'); 

をしかし、これはコールバックで動作します。静的にそれを行うには、一時的にそれをだますためにワードプレスのグローバルな状態を変更することができます。

/** 
* @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

-1

あなたはどのショートコードを除いたコンテンツのみを取得したい場合は、のようなものを試してみてくださいその

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