My function to detect is a gallery is on a page has broken and Im not sure why. So in my theme template's attachment page Im calling this function:

global $post;

$parent_id = $post->post_parent;

if ( strpos(get_post($parent_id)->post_content,'[gallery ') == false ){
    $navigation_markup = 'no gallery';
}else{ $navigation_markup = 'theres a gallery!' }

It USED to work, but last night I noticed that it stopped and I'm not sure why.

if the only (or first) thing in a post/page is [gallery id="1,2,3,4,5"] my function fails. However, if I add a space, or any character really it passes and detects the gallery.

So something about [gallery being in the very first position returns false.

2 Answers 2


It happens because strpos returns 0 (zero index position) when gallery shortcode is at the start of the content which is treated as false by the if condition.

Solution is to use Identical operator === in condition to check if the return value is a boolean false.


With WordPress 3.6 you can use this new function :

if( function_exists('has_shortcode') 
    && has_shortcode( get_post($parent_id)->post_content, 'gallery' )  )     {
    } else {
       //maybe your function as fallback for previous versions of WP

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.