2010-03-20 64 views
5

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

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

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

我仍然无法弄清楚如何显示当前图像的数量。
任何人有任何建议吗?

更新2 ...

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

“图片1 8Image 2 8Image 3的 8Image 4 8Image 5 8Image 6的 8Image 7的8" 8Image 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++; 
} 

怎么回事?

Update 3 - THE ANSWER!

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}"; 

回答

1

这工作:

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}"; 
0

这样添加一些东西到上面的代码:

$currentImage = 1; 
foreach ($attachments as $attachment) { 
    // output your image here 
    echo "Image ". $currentImage . " of ". $count; 
    $currentImage++; 
} 
+0

感谢您的答复。这让我几乎在那里。我已经用当前的问题更新了我的问题。 – mattz 2010-03-21 15:08:16

0

如果你正在寻找一个插件来管理图片库,你可以使用attachments插件,

http://wordpress.org/plugins/attachments/

它保持了画廊的独立性,并且不会将图像库简码放入发布内容中,因此可以让您完全控制图像显示在您的文章/页面/自定义文章中。您还可以通过只需将正下降

这里是如何检索您的画廊图像的示例代码更改图像的顺序,

<?php $attachments = new Attachments('attachments'); /* pass the instance name */ ?> 
<?php if($attachments->exist()) : ?> 
    <h3>Attachments</h3> 
    <p>Total Attachments: <?php echo $attachments->total(); ?></p> 
    <ul> 
    <?php while($attachments->get()) : ?> 
     <li> 
     ID: <?php echo $attachments->id(); ?><br /> 
     Type: <?php echo $attachments->type(); ?><br /> 
     Subtype: <?php echo $attachments->subtype(); ?><br /> 
     URL: <?php echo $attachments->url(); ?><br /> 
     Image: <?php echo $attachments->image('thumbnail'); ?><br /> 
     Source: <?php echo $attachments->src('full'); ?><br /> 
     Size: <?php echo $attachments->filesize(); ?><br /> 
     Title Field: <?php echo $attachments->field('title'); ?><br /> 
     Caption Field: <?php echo $attachments->field('caption'); ?> 
     </li> 
    <?php endwhile; ?> 
    </ul> 
<?php endif; ?>