metalbug
metalbug

Reputation: 17

How to get the second article's attached images in Wordpress

This function can't show second image. I just want the second image for the post attachments.

I just want attachment src, something like: 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].'"/>';
            }
        }
    }
}

I need the address of the second picture.

Upvotes: 0

Views: 477

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

I haven't test it, but try this:

$pics = count($attachments, COUNT_RECURSIVE ); // For multidimensional array.

Upvotes: 0

Related Questions