1

特定の投稿から、すべての画像を対応するキャプションとともに表示し、次のように順番に並べようとしています。

-------------
|           |
|    Img1   |
|           |
-------------
  Caption 1    

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 2        

-------------
|           |
|    Img3   |
|           |
-------------
  Caption 3        

それが私が達成したいことです。

コード:

<?php

$attachments = get_children( array(
'post_parent' => get_the_ID(), 
'order' => 'ASC', 
'post_mime_type' =>'image') );

$args = array(
'post_parent' => get_the_ID(),  
        'order' => 'ASC',
        'post_type' => 'attachment' ); 

$thumb_images = get_posts($args);


foreach ($attachments as $attachment_id => $attachment)  

foreach ($thumb_images as $thumb_image) 

{
{
  echo "<div class='image'>";
  echo wp_get_attachment_image( $attachment_id, 'full' );
  echo "</div>";
  echo "<div class='caption'>";  
  echo $thumb_image->post_excerpt;
  echo "</div>";
}
}

?>

3 つの画像とそれに対応するキャプションがある場合、このコードは各画像を 3 回表示し、それぞれに 3 つの異なるキャプションを付けます。つまり、9 つの画像と 9 つのキャプションです。少なくともキャプションは整っていますが、画像が繰り返されます。

-------------
|           |
|    Img1   |
|           |
-------------
 Caption 1    

-------------
|           |
|    Img1   |
|           |
-------------
 Caption 2        

-------------
|           |
|    Img1   |
|           |
-------------
 Caption 3


-------------
|           |
|    Img2   |
|           |
-------------
  Caption 1    

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 2        

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 3            


  ETC

コードが次のように更新された場合:

   <?php

$attachments = get_children( array(
'post_parent' => get_the_ID(), 
'order' => 'ASC', 
'post_mime_type' =>'image') );

$args = array(
'post_parent' => get_the_ID(),  
        'order' => 'ASC',
        'post_type' => 'attachment' ); 

$thumb_images = get_posts($args);


foreach ($attachments as $attachment_id => $attachment)   {

foreach ($thumb_images as $thumb_image)     {}  


  echo "<div class='image'>";
  echo wp_get_attachment_image( $attachment_id, 'full' );
  echo "</div>";
  echo "<div class='caption'>";  
  echo $thumb_image->post_excerpt;
  echo "</div>";

}

?>

繰り返しなしで画像を表示しますが、キャプションは最後に読み込まれた画像に属し、投稿に関連付けられた画像の合計量に相当する量を繰り返します。

-------------
|           |
|    Img1   |
|           |
-------------
  Caption 3    

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 3        

-------------
|           |
|    Img3   |
|           |
-------------
  Caption 3 

画像の数のx倍とキャプションの数のx倍で終わるように、正しく書く方法についてのアイデアはありますか? 繰り返しなし。

よろしくお願いします。

ローラ

4

1 に答える 1

0

$attachmentsそれが と同じサイズであると仮定すると$thumb_images:

$i = 0;
foreach ($attachments as $attachment_id => $attachment) {
    echo "<div class='image'>";
    echo wp_get_attachment_image($attachment_id, 'full');
    echo "</div>";
    echo "<div class='caption'>";  
    echo $thumb_images[$i]->post_excerpt;
    echo "</div>";
    $i++;
}

$attachmentsとの内容をよりよく理解するには$thumb_images、次のコード スニペットを使用してデバッグします。

echo "<pre>";
echo var_dump($attachments);
echo var_dump($thumb_images);
echo "</pre>";

$thumb_images配列であることがわかります。

Q: のロジックは$i++何ですか?

$i++ポストインクリメント演算子です。

$i++は の省略形な$i = $i + 1ので、ループの反復ごとに の値が$iインクリメントされます。最初は、$i = 0. が$i++呼び出されると、$i = 1. $i++が別の反復で再度呼び出された場合$i = 2など。

ループを使用する代わりに、ループを使用して配列を反復処理するforeachこともできます。ループforの例(およびそれに対応する概念を使用する例) については、PHP のドキュメントを参照してください。for$i++for

于 2012-06-12T23:19:40.103 に答える