从任何特定的帖子中,我试图显示所有图像及其相应的标题,并将它们一个接一个地排列,例如:
-------------
| |
| 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 倍的标题数量结束?没有重复。
最好的祝福。
劳拉