How do I test whether [gallery] is empty?

You can check if an attachment for the given post exists with the get_posts function. This will create a new sql query – so there could be a performance issue.


// Util.class.php
class Util {
     public static function has_attachments() {
         $args = array(
            'post_type' => 'attachment', 
            'numberposts' => -1, 
            'post_status' => null, 
            'post_parent' => $post->ID
         );

         $attachments = get_posts($args);

         if(!empty($attachments)) {
             return TRUE;
         } else {
             return FALSE;
         }
     }
}

// In template
require_once 'Util.class.php';
if (Util::has_attachments()) {
    // Display gallery
}