如何在Wordpress中获得第二篇文章的附件图像>>

问题描述 投票:0回答:1

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

我只想要附件src,类似:如果img = 1则echo img src =“ img1src”,如果img = 2则echo img src =“ img1src” src =“ img2src”以及img = 3则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].'"/>';
            }
        }
    }
}

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

此功能无法显示第二张图像。我只想要帖子附件的第二张图片。我只想要附件src,例如:if post img = 1 echo img src =“ img1src”,如果post img = 2 echo ...

wordpress image attachment
1个回答
0
投票

我还没有测试,但是尝试一下:

© www.soinside.com 2019 - 2024. All rights reserved.