5

我只想删除[gallery]我博客文章中的简码。我找到的唯一解决方案是添加到我的函数中的过滤器。

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

它删除了所有简码,包括[caption]我需要的图像。如何指定要排除或包含的单个短代码?

4

3 回答 3

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);
于 2012-02-25T02:57:11.130 回答
0

对我来说,曾与:

add_shortcode('shortcode_name', '__return_false');

如果我尝试剥离短代码,它们将删除所有短代码并更改最终结果

于 2021-07-16T20:53:03.087 回答
-1

如果您只想获取内容,不包括任何短代码,请尝试类似的操作

global $post;
$postContentStr = apply_filters('the_content', strip_shortcodes($post->post_content));
echo $postContentStr;
于 2013-01-10T19:46:22.667 回答