The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Check if post has gallery (WordPress)

Is there any way to check if a post has a gallery? I'm using a plugin that adds a jquery thickbox to galleries.. but it loads jQuery and the thickbox scripts on every page even though posts with galleries are the only pages that will use those javascripts. I don't want my users to download something they might not need during their visit

right, the statement evaluates to true. That's why the code I posted echos 'true' on a post with a gallery.

If it isn't true it shouldn't even run the wp_enqueue_script functions, but those scripts are still showing up on pages without a gallery. When I replace it with your snippet of code it displays <!-- TRUE --> on all pages, regardless if they have a gallery or not. Could it be that gallery_shortcode($post->ID) is returning something?

gallery_shortcode($post->ID) will return something if there are images attached to the post, but regardless of whether the [gallery] shortcode was used in the post. Since I use images in pretty much every post, it will always return false

I just tried that first piece of code you pasted, if (do_shortcode('[gallery]')), and it worked for me. What was the problem with using that?

It gave me some php error the last time I tried it, I'll try again tonight though. But I think it will give the same result as gallery_shortcode($post->ID) since I add at least one image to every post.

One thing I could do is make a function that goes through the post contents and searches for "[gallery]".. an awful lot to do just to get rid of those scripts

It usually wouldn't bug me that much, but it loads 8 javascripts just for this one feature that I seldom use. If it was all in one file it wouldn't bug me, but it just adds another possible issue by making the browser load 8 more files (and it can only load 2 scripts in parallel)

EDIT: Yeah, do_shortcode('[gallery]') does pretty much the same thing as gallery_shortcode($post->ID)