如何测试[图库]是否为空?

时间:2011-03-09 作者:gillespieza

如何测试是否有任何图像附加到帖子?

我正在使用gallery快捷码,但我想将其置于条件中,以便在gallery为空时不会显示周围的样式。

2 个回复
最合适的回答,由SO网友:Roman 整理而成

您可以使用get\\u posts函数检查给定帖子的附件是否存在。这将创建一个新的sql查询,因此可能存在性能问题。


// 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
}

SO网友:Eric
if ( gallery_shortcode( $post->ID ) ) echo do_shortcode( \'[gallery]\' );
结束

相关推荐

User-based media gallery

我正在建立一个同时使用WordPress和BuddyPress的网站,我希望建立一个自定义部分,用户可以上传作品,人们可以对其进行评论/评分。我浏览了各种不同的选项,但似乎使用自定义的帖子类型,然后允许成员提交内容是最好的选择。这是更好的方法吗?还是你认为帖子类型是最好的选择。要了解其所需的功能类型,请执行以下操作:http://dribbble.com/其想法是将图像作为类似域的URL。com/designs/my-awesome-work-2201/当查看成员档案时,您将能够查看他们的最新作品等,因此