SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
-
Aug 31, 2008, 15:13 #1
- Join Date
- Jul 2008
- Posts
- 140
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
I was thinking of doing something like:
Code PHP:if (do_shortcode('[gallery]')) { //load javascripts }
or
Code PHP:$checkgallery = do_shortcode('[gallery]'); if (!empty($checkgallery) { //load javascripts }
-
Sep 2, 2008, 14:32 #2
- Join Date
- Mar 2006
- Posts
- 367
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
try something like this
PHP Code:if (gallery_shortcode($post->ID)){
//load javascript
}
Aaron
On Twitter
-
Sep 2, 2008, 14:58 #3
- Join Date
- Jul 2008
- Posts
- 140
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm, it works in the templates, but not the plugin file:
Code PHP:function add_lightbox_gallery_jquery() { global $post; if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) { wp_enqueue_script( 'jquery'); wp_enqueue_script('dimensions', '/wp-content/plugins/lightbox-gallery/js/jquery.dimensions.js', array('jquery')); wp_enqueue_script('bgtiframe', '/wp-content/plugins/lightbox-gallery/js/jquery.bgiframe.js', array('jquery')); wp_enqueue_script('lightbox', '/wp-content/plugins/lightbox-gallery/js/jquery.lightbox.js', array('jquery')); wp_enqueue_script('tooltip', '/wp-content/plugins/lightbox-gallery/js/jquery.tooltip.js', array('jquery')); wp_enqueue_script('lightbox-gallery', '/wp-content/plugins/lightbox-gallery/lightbox-gallery.js', array('jquery')); } }
-
Sep 2, 2008, 16:12 #4
- Join Date
- Mar 2006
- Posts
- 367
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
it looks like an issue with your calls to wp_enqueue_script. since this works
PHP Code:function add_lightbox_gallery_jquery() {
global $post;
if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) {
echo '<!-- true -->';
}
}
Aaron
On Twitter
-
Sep 2, 2008, 17:19 #5
- Join Date
- Jul 2008
- Posts
- 140
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Sep 2, 2008, 18:01 #6
- Join Date
- Mar 2006
- Posts
- 367
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Aaron
On Twitter
-
Sep 2, 2008, 19:00 #7
- Join Date
- Jul 2008
- Posts
- 140
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
Using WP 2.6.0
Code PHP:function add_lightbox_gallery_jquery() { global $post; if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) { wp_enqueue_script( 'jquery'); wp_enqueue_script('dimensions', '/wp-content/plugins/lightbox-gallery/js/jquery.dimensions.js', array('jquery')); wp_enqueue_script('bgtiframe', '/wp-content/plugins/lightbox-gallery/js/jquery.bgiframe.js', array('jquery')); wp_enqueue_script('lightbox', '/wp-content/plugins/lightbox-gallery/js/jquery.lightbox.js', array('jquery')); wp_enqueue_script('tooltip', '/wp-content/plugins/lightbox-gallery/js/jquery.tooltip.js', array('jquery')); wp_enqueue_script('lightbox-gallery', '/wp-content/plugins/lightbox-gallery/lightbox-gallery.js', array('jquery')); } }
-
Sep 2, 2008, 19:09 #8
- Join Date
- Jul 2008
- Posts
- 140
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Alright, I did:
Code PHP:function add_lightbox_gallery_jquery() { global $post; if (gallery_shortcode($post->ID) && strpos($_SERVER['REQUEST_URI'], 'admin.php') === false ) { echo '<!--' . gallery_shortcode($post->ID) . '-->'; } }
And it returned:
Code HTML4Strict:<!-- <style type='text/css'> .gallery { margin: auto; } .gallery-item { float: left; margin-top: 10px; text-align: center; width: 33%; } .gallery img { border: 2px solid #cfcfcf; } .gallery-caption { margin-left: 0; } </style> <!-- see gallery_shortcode() in wp-includes/media.php --> <div class='gallery'><dl class='gallery-item'> <dt class='gallery-icon'> <a href='http://gameviper.com/ps3/resistance-2-release-date-collectors-edition-global-multiplayer-and-co-op-beta-confirmed/attachment/resistancethumb' title='resistancethumb'><img src="http://gameviper.com/wp-content/uploads/2008/09/resistancethumb.jpg" width="77" height="100" class="attachment-thumbnail" alt="" /></a> </dt></dl> <br style='clear: both;' /> </div> -->
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
-
Sep 2, 2008, 19:54 #9
- Join Date
- Mar 2006
- Posts
- 367
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Aaron
On Twitter
-
Sep 2, 2008, 22:27 #10
- Join Date
- Jul 2008
- Posts
- 140
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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)
-
Sep 3, 2008, 08:51 #11
- Join Date
- Mar 2006
- Posts
- 367
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hmm... I would think there would be an easier way to do this. $array = shortcode_parse_atts($post->post_content); will give you an array you can itterate through to check for [gallery]
Best of luck.Aaron
On Twitter
Bookmarks