Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
1  
Do you mean get_permalink( $attachment->ID ) )? –  toscho 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. :) –  toscho Aug 1 '12 at 10:00
    
If something doesn’t work look for debug data. –  toscho Aug 1 '12 at 10:01

4 Answers 4

up vote 1 down vote accepted

The the_attachment_link returns html link so use this code

<?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_link( $attachment->ID, '' , true, false, 'Link to image attachment' );
}

?>

here I'm passing 5 paramters 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.
share|improve this answer
    
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

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 ) . '';
}

?>
share|improve this answer

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

share|improve this answer

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().

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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