2012-02-25 21 views
5

Blog gönderilerimdeki sadece [gallery] kısa kodları şeritlemek istiyorum. Bulduğum tek çözüm, işlevlerime eklediğim bir filtreydi.Wordpress şeridi gönderilerden tek kısa kod

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

Ben görüntüler için ihtiyaç [caption] dahil tüm kısa kod kaldırır. Hariç tutmak veya eklemek için tek bir kısa kod nasıl belirleyebilirim?

+0

Bir kısa kod veya iki örnek verebilir misiniz? –

cevap

13

, sadece galeri shortcode kaldırmak boş bir dize döndüren bir geri çağırma işlevi kaydetmek için:

add_shortcode('gallery', '__return_false'); 

Ama bu sadece geri aramaları ile birlikte çalışacaktır. statik bunu yapmak için, geçici olarak kandırmak için wordpress küresel durumunu değiştirebilirsiniz:

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

Kullanımı: Herhangi bir kısa kod hariç, sadece içeriği almak istiyorsanız

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

Güzel bir tane ama diğer kısa kodları geri göndermeniz gerekiyor, daha sonra echo do_shortcode ($ content) unutmayın. – Benn

-1

gibi bir şey deneyin

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