5

我使用图像附件页面以幻灯片形式的效果一张一张地显示附加到帖子的图像。我希望能够显示附加到父帖子的图像总数以及在任何给定附件页面上显示的特定图像的数量,以便您看到图片和“图片 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...

格林尼的回答让我几乎到了那里,但它一次输出了所有数字:

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

3 回答 3

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}";
于 2010-03-21T22:55:45.250 回答
0

在上面的代码中添加类似这样的内容:

$currentImage = 1;
foreach ($attachments as $attachment) {
   // output your image here
   echo "Image ". $currentImage . " of ". $count; 
   $currentImage++; 
}
于 2010-03-21T12:35:58.523 回答
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; ?> 
于 2013-07-01T11:46:04.530 回答