0

i've been trying to get the attachment URL on single.php, so far this code gets the DIRECT LINK to the image;

<?php

if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo '<a target="_blank" href="' . wp_get_attachment_url( $attachment->ID ) . '">Download Full Size</a>';
}

?>

but instead i want the attachment URL, PLEASE HELP!

4
  • 1
    Do you mean get_permalink( $attachment->ID ) )? – fuxia Aug 1 '12 at 9:39
  • Fatal error: Call to undefined function wp_get_permalink() – XeBii Aug 1 '12 at 9:54
  • Of course, such a function doesn’t exists … and no one said it does. :) – fuxia Aug 1 '12 at 10:00
  • If something doesn’t work look for debug data. – fuxia Aug 1 '12 at 10:01
2

The the_attachment_link returns an HTML link, so use this code:

if ( $attachments = get_children( array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'numberposts'    => 1,
    'post_status'    => null,
    'post_parent'    => $post->ID
) ) );
foreach ( $attachments as $attachment ) {
    echo wp_get_attachment_link( 
        $attachment->ID, '' , true, false, 'Link to image attachment' 
    );
}

Here I'm passing 5 parameters to function wp_get_attachment_link()

  • First parameter is $attachment->ID to get attachment ID
  • Second parameter '' says not to print image
  • Third parameter true links to attachment page
  • Fourth false parameter tells not to print media parameter
  • The last one is the text you'd like to appear as a link
4
  • This function gets the attachment as thumbnail and linked to Direct link (.jpg), i want the link in HTML format to attachment page so where i can maximize my images to view larger – XeBii Aug 1 '12 at 10:09
  • hey, updated answer to print link to attachment page with text. – amit Aug 1 '12 at 10:30
  • set the $permalink parameter to true to return link to attachment page instead of file. – amit Aug 1 '12 at 10:40
  • :D Problem Solved! Thanks alot amit for helping me out :) – XeBii Aug 1 '12 at 12:39
2

You’re looking for: get_attachment_link( $attachment->ID ).


As toscho suggested in his comment, you can also use the generic get_permalink() function which internally calls get_attachment_link().

3
  • Tried this one but it gives me "Missing Attachment" – XeBii Aug 1 '12 at 9:50
  • If no attachment can be found, the function displays the string Missing Attachment. – amit Aug 1 '12 at 9:51
  • Attachment is there, if you see above code that i posted shows the Direct link to the image that has been attached in the POST – XeBii Aug 1 '12 at 9:53
0

Great! changing the lines

'post_mime_type'=>'image', 
  echo wp_get_attachment_link( $attachment-  >ID, '' , true, false, 'Link to image attachment' );

to

 'post_mime_type'=>'application', 
  echo wp_get_attachment_link( $attachment->ID, '' , false, false, 'Download File' );

I could link a direct download from PDF File attached to post

0

I was looking for the code that can able to pull exact URL of corresponding image from media and found this thread. I tweaked it little bit

Here is the code that I used to pull image for PIN IT button.....

<?php

if ( $attachments = get_children( array(
   'post_type' => 'attachment',
   'post_mime_type'=>'image',
   'numberposts' => 1,
   'post_status' => null,
   'post_parent' => $post->ID
)));

foreach ($attachments as $attachment) {
   echo '' . wp_get_attachment_url( $attachment->ID ) . '';
}

?>

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.