1

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:

[gallery ids="1,2"] // hide
[gallery ids="3,4"] // display 
[gallery ids="5,6"] // display

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

#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.

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.