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'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

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
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. –  Darren 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
add comment

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.