How to hide first gallery for every post?

I’m using galleries for attaching images, so the very first gallery for every post is used to generate a slider and nothing more. That’s why I don’t want to see it in post’s conent. Let’s say I have a single post that looks like this:

 // hide
 // display 
 // display

Is there some way of blocking the very first gallery from the content for every post?

Read More
#gallery-1 { display: none !important; } 

works good but is ugly as hell 🙂 Also it only hides the gallery but it’s still there, same goes for it’s JS.

Related posts

1 comment

  1. You could use preg_replace() but I think it’s much easier to overwrite the $output via the post_gallery filter and then remove the filter after the first run:

    /**
     * Remove the output of the first gallery
     *
     * @param string $output
     * @param array $attr
     * @return string $output
     */
    function wpse125903_remove_the_first_gallery( $output, $attr )
    {
        // Run only once
        remove_filter( current_filter(), __FUNCTION__ );
    
        // Override the first gallery output        
        return '<!-- gallery 1 was here -->';   // Must be non-empty.
    }
    
    add_filter( 'post_gallery', 'wpse125903_remove_the_first_gallery', 10, 2 );
    

    This should remove only the first gallery in your posts.

Comments are closed.