【问题标题】:Strip WordPress gallery shortcode from the_content and use outside the loop从 the_content 中剥离 WordPress 图库短代码并在循环外使用
【发布时间】:2014-03-06 14:16:09
【问题描述】:

我希望能够从 the_content 中删除 wordpress 图库短代码,然后在循环之外再次使用该短代码。

我目前正在使用以下方法在 functions.php 中剥离短代码:

add_filter('the_content', 'strip_shortcodes');

然后在我的模板中的循环之外使用它:

<?php echo do_shortcode('[gallery"]'); ?>

这很好用,只是它会删除所有简码,包括我需要在帖子中使用的标题简码。

有没有人知道如何解决这个问题?

提前致谢。

【问题讨论】:

    标签: wordpress gallery shortcode caption


    【解决方案1】:
    // 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;
    }
    

    【讨论】:

    • 这应该有更多的赞成票。我已经阅读了几种不同的方法来剥离画廊短代码,这绝对是最简洁的!完美运行,谢谢!
    • =/ 唯一的问题是这也会删除所有内容样式,例如


    猜你喜欢
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多