如何在图片附件页面上显示附加到帖子的图片数量? [英] How do I show the number of images attached to a post on the image attachment page?

查看:23
本文介绍了如何在图片附件页面上显示附加到帖子的图片数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用图像附件页面以幻灯片形式一张一张地显示附加到帖子的图像.我希望能够显示附加到父帖子的图像总数以及在任何给定附件页面上显示的特定图像的数量,以便您看到图片和文字 "Image 3 of 15" 例如.

I use the image attachment page to show images attached to a post one by one, in a slideshow sort of affect. I'd like to be able to display the total number of images attached to the parent post and the number of the particular image that's being shown on any given attachment page so you see the picture and the words "Image 3 of 15" for example.

更新...我能够使用此代码获得要显示的总数:

Update... I was able to get the total number to show using this code:

<?php 
  global $post;
  $attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
  $count = count( $attachments );
  echo $count; 
?>

我仍然不知道如何显示当前图像的编号.
有人有什么建议吗?

I still can't figure out how to show the number of the current image.
Anyone have any suggestions?

更新 2...

Greenie 的回答让我几乎到了那里,但它同时输出了所有数字:

Greenie's answer got me almost there but it's outputting all the numbers at once:

"8 的图像 1 8 的图像 2 的图像 38图像 4 的 8 图像 5 的 8 图像 68Image 7 of 8Image 8 of 8"

"Image 1 of 8Image 2 of 8Image 3 of 8Image 4 of 8Image 5 of 8Image 6 of 8Image 7 of 8Image 8 of 8"

这是我使用的代码:

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$currentImage = 1;
foreach ($attachments as $attachment) {
   // output your image here
   echo "Image ". $currentImage . " of ". $count; 
   $currentImage++; 
}

怎么了?

更新 3 - 答案!

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
    $specific[$attachment->ID] = $i;
    ++$i;
}

echo "Image {$specific[$post->ID]} of {$count}";

推荐答案

这有效:

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
    $specific[$attachment->ID] = $i;
    ++$i;
}

echo "Image {$specific[$post->ID]} of {$count}";

这篇关于如何在图片附件页面上显示附加到帖子的图片数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆