1

当帖子在主页上列出时,我想阻止显示帖子的画廊。

我认为它会在主页上发布时add_filter使用。apply_filter

您可以通过单击添加媒体按钮将图库添加到帖子中。您可以选择现有图像或上传将在帖子中创建图库的其他图像。这嵌入了一个短代码$post['content'],看起来像[gallery ids="37,38,39,40,41,42].

问题是,默认情况下,它会在帖子包含在主页以及单个帖子本身时显示。

更新:这就是我现在正在做的实现要求。我怀疑会有更优雅的方式。

        <div class="entry-content">
        <!-- Begin Post Content -->
                    <?php if ( is_single() ) : ?>
                    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'faceboard' ) ); ?>
                    <?php else : // Filter Gallery ShortCode out ?>
                    <?php 
                        $content = '';
                        $content = get_the_content(); 
                        $content = preg_replace('/\[gallery\sids="[0-9]+(,[0-9]+)*,?"\s?(royalslider="\d")?\]/s',"",$content);
                        echo wpautop( $content, 1);
                    ?>
                    <?php endif; // is_single() ?>
        <!-- End Post Content -->
        <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'faceboard' ), 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
4

1 回答 1

0

您可以将以下内容添加到您的主题functions.php文件或自定义插件中(更好,因为您可以在不触及主题的情况下禁用它)。

该过滤器post_gallery用于创建您自己的画廊,该$content参数默认为空。如果它返回空,则处理原始[gallery]短代码。
在这里,我们使用了一个虚拟的空值,所以过滤器被欺骗认为我们正在传递一些实际的画廊内容,但它只是一个空白。

add_filter( 'post_gallery', 'disable_home_galleries_so_17635042', 10, 2 );

function disable_home_galleries_so_17635042( $content, $atts )
{
    // http://codex.wordpress.org/Conditional_Tags
    if( is_home() )
        return '&nbsp;';

    return $content;
}
于 2013-07-14T01:22:15.403 回答