drogon982007 2013-10-24 12:21
浏览 21

wordpress计数特定帖子中的图像数量

I am having a wordpress loop that shows all the posts. I need to have a count of all the images for a particular post.

<?php while ( $the_query->have_posts() ) : $the_query->the_post();
  $attachments = get_children( array( 'post_parent' => $post->ID, 'post_mime_type' => 'image' ) );

    $count = count( $attachments );
    $specific = array();
    $i = 1;
          foreach ( $attachments as $attachment ) {
         $specific[$attachment->ID] = $i;
    ++$i;
    }  ?> <a href="#"><?php echo $count; ?></a>
      <?php endwhile; ?>

the problem is that, it doesn't works and it does not display images count.

  • 写回答

3条回答 默认 最新

  • douwei2825 2016-11-14 00:42
    关注

    here is a solution and for more information visit https://blog.josemcastaneda.com/2014/03/18/get-image-count-in-wordpress-post/

    // Get all the galleries in the current post
        $galleries = get_post_galleries( get_the_ID(), false );
        // Count all the galleries
        $total_gal = count( $galleries );
        /**
         * count all the images
         * @param array $array The array needed
         * @return int returns the number of images in the post
         */
        function _get_total_images( $array ){
            $key = 0;
            $src = 0;
            while ( $key < count( $array ) ){
                $src += count( $array[$key]['src'] );
                $key++;
            }
            return intval( $src );
        }
    
        echo _get_total_images( $galleries );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线