0

During the post creation of a post in Wordpress, I add 2 pictures in the post Gallery. How can I retrieve uploaded pictures URLs to display them on the other template?

0

When you upload an attachment from the post edit screen, WordPress set the current post ID as the post_parent of the attachment. And that's how you get these attachments:

$post_ID = 123;
$attachments = get_posts(
    array(
        'post_type'      => 'attachment',
        'post_status'    => 'inherit',
        'post_parent'    => $post_ID,
        'post_per_page'  => -1,
        'post_mime_type' => 'image/*'
    )
);
foreach ( $attachments as $a ) {
    $info = wp_get_attachment_image_src( $a->ID, 'full' );
    $info[ 0 ]; // url to the image
    $info[ 1 ]; // image with in px
    $info[ 2 ]; // image height in px
    $info[ 3 ]; // TRUE if the image is a resized one
    dump( $info[ 0 ] );
}

Codex for wp_get_attachment_image_src()

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.