2012-02-25 3 views
5

내 블로그 게시물에 [gallery] 단축 코드 만 제거하고 싶습니다. 내가 찾은 유일한 해결책은 필자의 기능에 추가 한 필터입니다.글의 Wordpress 스트립 단일 단축 코드

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

이미지에 필요한 [caption]을 포함한 모든 shortcodes를 제거합니다. 하나의 단축 코드를 제외 또는 포함하도록 어떻게 지정할 수 있습니까?

+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;