【问题标题】:NextGen Plugin - Display Gallery Image CountNextGen 插件 - 显示图库图像计数
【发布时间】:2012-11-05 19:58:24
【问题描述】:

我正在为网站使用 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

有人有什么建议吗?

谢谢, 伊恩

【问题讨论】:

  • 你的意思是你想把所有图片附加到你画廊里的每个帖子上?
  • 我只是想获取附加到每个画廊的图像并在每个画廊缩略图下显示计数。
  • 画廊的图片来自帖子对吗?所以你必须从帖子中获取所有图片?我说的对吗?
  • 它们来自 NextGen 管理屏幕中的画廊。我不相信它们会作为帖子附件处理。

标签: image wordpress gallery


【解决方案1】:

希望我的解决方案对你有用。

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

$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") );

【讨论】:

  • 我试过了,但我无法获得 per 库的图像。它只打印所有画廊中的图像总数。我怎样才能让它打印出每个画廊的图像数量? &lt;?php global $wpdb; $images = intval( $wpdb-&gt;get_var("SELECT COUNT(*) FROM $wpdb-&gt;nggpictures") ); ?&gt; &lt;a rel="prettyPhoto" href="&lt;?php echo $image-&gt;imageURL ?&gt;" &lt;?php $image-&gt;thumbcode ?&gt;&gt;&lt;span&gt;view&lt;/span&gt;&lt;/a&gt; &lt;?php echo $images; ?&gt; pictures
【解决方案2】:

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

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

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

这是一个阴险、阴险的插件。

【讨论】:

    【解决方案3】:

    如果您无法计算图库中的图像,则可以计算 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>
    

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      我使用这个小技巧从我粘贴在帖子中的简码 ([nggallery=1]) 中获取我的画廊 ID

      <?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!
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多