Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

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.

share|improve this answer

With WordPress 3.6 you can use this new function :

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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