基本的に、Wordpress のコンテンツからギャラリーのショートコードを削除する必要があります。
echo preg_replace('/\[gallery ids=[^\]]+\]/', '', get_the_content() );
ギャラリーのショートコードが正常に削除されていますが、保持する必要がある段落タグも削除されています。アイデアは、ギャラリー以外のコンテンツのすべてを出力したいということです。
ショートコードまたはショートコードの特定のリストを削除するには、このコードを使用できます。
global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* @global int $remove_shortcode
* @param type $shortcodes comma seprated string, array of shortcodes
* @return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;
if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;
$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
dot1_strip_shortcode( 'gallery' );
コンマで区切られた単一のショートコード文字列またはショートコードの配列を受け入れます。