我希望能够从 the_content 中删除 wordpress gallery 短代码,然后在循环之外再次使用该短代码。
我目前正在使用以下方法在functions.php中剥离短代码:
add_filter('the_content', 'strip_shortcodes');
然后在我的模板循环之外使用它:
<?php echo do_shortcode('[gallery"]'); ?>
这非常有效,只是它会删除所有短代码,包括我需要在帖子中使用的标题短代码。
有人知道如何解决这个问题吗?
提前致谢。
最佳答案
// Remove gallery from content
add_filter('the_content', 'strip_shortcode_gallery');
function strip_shortcode_gallery( $content ) {
preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
if ( ! empty( $matches ) ) {
foreach ( $matches as $shortcode ) {
if ( 'gallery' === $shortcode[2] ) {
$pos = strpos( $content, $shortcode[0] );
if ($pos !== false)
return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
}
}
}
return $content;
}
关于wordpress - 从 the_content 中剥离 WordPress 画廊短代码并在循环之外使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227106/