0

此功能无法显示第二张图片。我只想要帖子附件的第二张图片。

我只想要附件 src,例如: if post img =1 echo img src="img1src" and if post img=2 echo img src="img1src" src="img2src" and if img=3 echo img1src img2src img3src.. .

function the_images()
{
    global $post;
    $attachments = get_children(['post_type'      => 'attachment',
                                 'numberposts'    => -1,
                                 'post_mime_type' => 'image',
                                 'post_status'    => null,
                                 'post_parent'    => $post->ID,
    ]);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $src = wp_get_attachment_image_src($attachment->ID, "attached-image");
            $pics = count($attachments);
            if ($pics == 2) {
                echo '<img src="'.$src[0].'"/><img src="'.$src[1].'"/>';
            }
        }
    }
}

我需要第二张图片的地址。

4

1 回答 1

0

我还没有测试它,但试试这个:

$pics = count($attachments, COUNT_RECURSIVE ); // For multidimensional array.
于 2012-12-03T05:22:03.653 回答