wordpress - 从 the_content 中剥离 WordPress 画廊短代码并在循环之外使用

标签 wordpress gallery shortcode caption

我希望能够从 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/

相关文章:

php - 无法访问序列化数据

wordpress - 如何在 WordPress 中向自定义帖子类型添加按钮?

android - 如何为 android 实现自定义设备照片库?

html - 悬停时图像上的缩放/缩放功能,悬停图像不会是 "on top"。我可以使用 CSS 吗?

php - 如何获取用户(客户)在 WooCommerce 中花费的总金额?

php - 在购物车 Woocommerce 中添加产品简短描述

php - WordPress:将小部件分配给各个页面

image - 如何在 Wordpress 中更改默认图像缩略图大小

wordpress - Elementor 页面构建器简码问题

wordpress - 简码出现在内容的顶部,而不是我需要的地方