Get the Wordpress attachment image url

I am trying to display all the images attached to a wordpress post and link the image to the source of the image to open it in a light box. I am using the following code:

if (have_posts())
{ while (have_posts()) { the_post(); $args = array( 'orderby' => 'title', 'order' => 'ASC', 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id() ); $attachments = get_posts( $args ); if ($attachments) { foreach ($attachments as $attachment) { ?> <div> <a rel="lightbox" href="<?php /* ??? */ ?>"> <?php echo wp_get_attachment_image ($attachment->ID, 'full'); ?> </a> </div> <?php } } }
}

I have tried many things where the ??? but i can't seem to retrieve just the source url.

Can anyone provide some insight?

2 Answers

Use wp_get_attachment_image_src

This:

wp_get_attachment_image_src( $attachment->ID, 'full' );

should return an array with the following elements

[0] => url
[1] => width
[2] => height
[3] => boolean: true if $url is a resized image, false if it is the original or if no image is available.

If you just want to link to the original full image it's better to simply use "wp_get_attachment_url" -

The wp_get_attachment_image_src function will run code you don't need and return extra details you don't need making your code less efficient.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like