NextGen插件 - 显示图库图像计数

时间:2012-11-17 04:24:30

标签: image wordpress gallery

我正在为网站使用NextGen图库WordPress插件。在我的gallery.php模板中,我想检索循环中显示的每个图库的图像数量。我无法弄清楚如何获取数据并将其打印在gallery.php中调用的每个库的缩略图下

这是我要插入图库图像计数并打印它的地方:

<a rel="prettyPhoto" href="<?php echo $image->imageURL ?>" 
<?php $image->thumbcode ?>><span>view</span></a> <?php echo $total_images; ?> pictures

有人有任何提示吗?

谢谢, 伊恩

4 个答案:

答案 0 :(得分:1)

我用这个小黑客从我粘贴在帖子中的短代码中获取我的画廊ID([nggallery = 1])

<?php
    $id = get_the_ID();//Get the id of the specific post (inside the loop)
    $string = get_the_content();//You get the whole post content where in the end of it lies your shortcode (for example [nggallery=1])
    if(preg_match_all('/=(.*?)\]/s',$string,$match)) {            
    $finalstring=$match[1][0];
    }// get the id only (gets all chars after '=' and before ']')
    global $wpdb;
    $total_attachments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures WHERE galleryid=$finalstring" ); // Voila!
?>

答案 1 :(得分:0)

希望我的解决方案能为您服务。

这是计算图像所需的代码:

$global $wpdb;
$images    = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures") );
$galleries = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggallery") );
$albums    = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggalbum") );

答案 2 :(得分:0)

答案是你不能。 您可以计算$ images变量中的键,但它将返回错误的数字。 您可以检索所有图像的总数,或所有无用的图库。 您甚至可以在循环查看阵列时使用计数器来尝试计算图库中有多少图像,但您仍然会得到错误的数字。 您可以尝试默认使用全局变量:images-&gt; total,它们是空的且未定义,甚至不存在于$ images或$ current对象中。

代码显示(&#34;图3/7)&#34;类型显示。如果您在imagebrowser.php模板中使用它,它可以工作。如果您在gallery-carousel.php模板中粘贴此完全相同的代码。不起作用。现在你可能会认为切换到$ current-&gt; total会起作用,因为当使用gallery_carousel时它对其余的变量莫名其妙地做了,但是没有,它没有。 答案是您必须直接从数据库中提取信息。

 <?php _e('Picture', 'nggallery') ?> <?php echo $image->number ?> <?php _e('of',      'nggallery')?> <?php echo $image->total ?>

这是一个险恶,阴险的插件。

答案 3 :(得分:0)

如果您无法计算图库中的图像,则可以计算html块上的标记。

在我的主题上,我从自定义字段中获取图库ID,并在模板内回显图库。所以,它就是这样的:

<?php
    $myGalleryId = 1; // example
    $displayImages = 0; // the number of images you want to display from gallery. 0 = all images 
    $newnggShortcodes = new NextGEN_Shortcodes;
    $htmlGallery = str_get_html($newnggShortcodes->show_gallery( array("id"=>$myGalleryId,"images"=>$displayImages,"template"=>"popular") ));
    $countImg = count($htmlGallery->find('img')); // number of total <img> tags inside the gallery
    $str = $htmlGallery;
?>
<!-- now the html -->
<div id="myGallery">
<?php echo $str; ?>
</div>
<span>Total images: <?php echo $countImg; ?></span>

希望它有所帮助。

相关问题