5

画像添付ページを使用して、投稿に添付された画像を 1 つずつ、スライドショーのような効果で表示します。親投稿に添付された画像の総数と、特定の添付ページに表示されている特定の画像の数を表示できるようにして、たとえば、画像と「15 の画像 3」という言葉を表示できるようにしたいと考えています。 .

更新... このコードを使用して表示する合計数を取得できました:

<?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's answer は私をほぼそこに導きましたが、一度にすべての数値を出力しています:

「画像 1/8 画像 2/8 画像 3/8 画像 4/8 画像 5/8 画像 6/8 画像 7/8 画像 8/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 に答える