I'd like to run some code only if a gallery (inserted with the [gallery] shortcode) has been inserted into a post/page. I did the following:

gallery_shortcode($post->ID) ?
$gallery = 1 : $gallery = 0;

However, this always sets $gallery = 0 whether there's a gallery or not. Am I using this incorrectly?

  • Normally that should be (no matter if it's working or not): $gallery = gallery_shortcode($post->ID) ? true : false;
    – kaiser
    Jun 16, 2011 at 23:38

3 Answers 3


try :

if (strpos($post->post_content,'[gallery') === false){
  $gallery = 0;
  $gallery = 1;
  • 2
    Just to make that (more) clear for later readers: 0 = false & 1 = true
    – kaiser
    Jun 16, 2011 at 23:37
  • Isn't it a kind of resource hog checking through lots of (probably) big texts for the occurence of [gallery ?
    – vlood
    Jan 14, 2012 at 16:23
  • 2
    @vlood Strpos is the fastest function there is when it comes to searching strings and not big on resources so that's not an issue.
    – Bainternet
    Jan 14, 2012 at 18:36
  • I was thinking of comparing strpos with something on database side... If you got a website with lot's of visitors daily and for some reason you don't have caching enabled, filtering posts on the query might be a lot better than doing that in the php code.
    – vlood
    Jan 22, 2012 at 19:28
  • 2
    Great, I added this to functions.php for re-use: function hasgallery(){global $post;return (strpos($post->post_content,'[gallery') !== false);} Jul 26, 2012 at 12:01

My iteration on the suggested solution here, is the following function in my theme's functions.php:

function has_gallery($post_id = false) {
    if (!$post_id) {
        global $post;
    } else {
        $post = get_post($post_id);
    return ( strpos($post->post_content,'[gallery') !== false); 

Which I can then call in a template file (doesn't care about The Loop):

$has_gallery = has_gallery($post_id) ? true : false;

or more direct:

has_gallery($post_id); //will evaluate to true/false

$post_id is optional, the following will only work within The Loop:


How about simply using the get_post_gallery() function. This is exactly the way, most themes are checking if a post has a gallery to print the correct body CSS classes:

if ( get_post_gallery() ) {
    // run your code in here

P.S. I really don't like solutions that search the whole content for anything, even if string functions are typically quite fast in PHP, it's still a huge performance overhead.

  • If you look into the code, get_post_gallery calls get_post_galleries which calls has_shortcode which does a strpos and some more... Doing a strpos from the start is way faster.
    – GUI Junkie
    Feb 12, 2015 at 17:00
  • That's correct. In terms of perfomance, the accepted answer is faster. But someone might have used another shortcode for galleries that hooks into the get_post_gallery() function. Therefore this solution would always work and there is also no need to implement an additional function.
    – 2ndkauboy
    Feb 12, 2015 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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