Preventing Display of Post Gallery when on Homepage

I would like to prevent the post’s gallery from displaying when the post is listed on the homepage.

I’m thinking it will utilize add_filter and apply_filter when post in on homepage.

Read More

You can add a gallery to posts by clicking the add media button. You can select existing images or upload additional images that will create a gallery within the post. This embeds a shortcode in $post['content'] that looks like .

The issue is that by default it displays when the post is included on homepage as well as the individual post itself.

Update: This is what I am doing right now to achieve the requirement. I suspect there will be a more elegant way.

        <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('/[gallerysids="[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 -->

Related posts

Leave a Reply

1 comment

  1. You can add the following to your theme’s functions.php file or to a custom plugin (better, as you can disable it without touching the theme).

    The filter post_gallery is used to create your own gallery, and the $content parameter comes empty by default. If it returns empty, the original [gallery] shortcode is processed.
    Here, we are using a dummy empty value, so the filter is tricked into thinking that we are passing some actual gallery content, but it’s just a white space.

    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;
    }