特定の投稿から、すべての画像を対応するキャプションとともに表示し、次のように順番に並べようとしています。
-------------
| |
| 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倍で終わるように、正しく書く方法についてのアイデアはありますか? 繰り返しなし。
よろしくお願いします。
ローラ