1

I'm using the following code to display a list of the posts attachments within a sidebar:

<ul>            
<?php $args = array(
'post_type' => 'attachment',
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
        echo '<p>This article has the following attachments:</p>';
        foreach ($attachments as $attachment) {
        echo '<li class="document">';
        the_attachment_link($attachment->ID, false);
        echo '</li>';}
        } else { 
        echo '<li class="nodocument">No documents attached to this article.</li>';
} ?>
</ul>

Most posts contain PDF's, Zip files and JPG/PNG files. The code above does list the attachments, however, when the attachment is an image it displays a thumbnail rather than just a link to the image.

Can anyone advise how I can go about making sure each attachment is listed as a link?

Thanks, Darren

3

Instead of the_attachment_link use the following:

echo wp_get_attachment_link($attachment->ID, 'full', false, false, true);

wp_get_attachment_link is what's being called internally by the_attachment_link, the last parameter set to true makes it output a text link instead of an image.

  • Thanks for your answer. Using your suggested code causes the title for each link to be "1" - very odd! However, I changed it to the following: echo wp_get_attachment_link($attachment->ID, false, false, false, false); now it returns the title of each attachment. Thanks very much for pointing me in the right direction. – D-B Sep 28 '11 at 11:44
  • Ah didn't actually test the code so that could be true ;) Glad you got it working! – Joost de Valk Sep 28 '11 at 11:46
  • one false should be enough :-) echo wp_get_attachment_link($attachment->ID,false); – krembo99 Jun 3 '12 at 3:42

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.