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');

strip_shortcode を実行しようとすると、すべてのショートコードが削除され、最終結果が変更されます

于 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 に答える