views:

20

answers:

1

I've been trying to come up with a way to add a list of PDF's from the Media Library in WordPress. It's for a list of newsletters which will start with the most recent, and list about 5. I want it to list with a title and that title be a hyperlink to the document. So when a visitor clicks the link it will download the PDF.

I have tried the "get_posts" route but can't figure it out at all. Currently what I'm doing is inserting the PDF's into a post with a category of "newsletter" and then having a separate loop to pull them up on the page. I've almost got this working, but it's not linking properly. So I was hoping for some help.

My code so far looks like this:

<h3>Newsletters</h3>
  <ul>
    <?php query_posts('category_name=newsletter&posts_per_page=5'); ?>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <li>
      <a href="<?php echo wp_get_attachment_url(the_ID()); ?>" title="<?php echo esc_attr( get_the_title() ); ?>" rel="attachment">
      <?php echo esc_attr( get_the_title() ); ?></a> 
    </li>                    
    <?php endwhile; ?>
  </ul>

The part I just can't seem to get right is:<?php echo wp_get_attachment_url(the_ID()); ?>

A: 

Currently what I'm doing is inserting the PDF's into a post with a category of "newsletter"

If the only PDFs you upload to your media library are the newsletter, then you can by-pass this step altogether and query the 5 most recent PDFs?

query_posts('post_mime_type=application/pdf');

The trouble you're having at the moment is you should be passing the ID of the attachment to wp_get_attachment_url(), not the post ID.

TheDeadMedic