This evening, I found myself in the unique position of needing to display a list of PDF files at the bottom of a WordPress page. At first, I attempted to simply upload all of the PDF files using the WordPress image uploader, and then inserting a “gallery” at the foot of my post.
Unfortunately, I soon figured out that the “gallery” only displays images. In my case, that meant a blank space where I wanted the list of PDF files to appear (since, obviously, none of them are images).
Then, I remembered that anything uploaded through the image uploader is stored as an “attachment” in the database. After some quick Googling, I found some information in the WordPress codex explaining basically how to insert a list of attachments in a page or post.
I inserted that code into my template file and checked out the page. Unfortunately, all I had was a jumbled blob of attachment names and links to the attachments. I wanted it formatted into a nice list and, since the “titles” of my attachments were set appropriately and were being used as the text of the link, it seemed silly to show the title twice.
I did a little more research and came up with a slightly better (in my opinion, at least) solution. This solution creates an unordered list of the PDF attachments in alphabetical order.
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'order_by' => 'title',
'order' => 'ASC'
);
$attachments = get_posts($args);
if ($attachments) {
echo '
<ul>';
foreach ($attachments as $attachment) {
$attlink = wp_get_attachment_link($attachment->ID);
/**
* A simple check to see if this includes a link to a PDF file
* Remove this conditional statement if you want to show all attachments
* Change the file extension if you want to show a different type
* of attachment
*
* If you remove this conditional, you should replace it with a
* conditional that checks to make sure the $attlink variable
* does not equal "Missing Attachment", as that is the string
* that is returned when there is no attachment associated with
* that ID.
*/
if(stristr($attlink,'.pdf')) {
echo '
<li>'.$attlink.'</li>
';
}
}
echo '</ul>
';
}
If you plan to use this only on a single page, I would recommend adding a conditional statement (using either is_page()
or is_post()
) inside your main theme file that checks to see if the user is viewing the specific page on which you want to show the list of PDF files.
If you plan to make this available for multiple pages, you might want to add a Custom Field to each of those pages/posts and then check within your template file to see if that custom field is set. Of course, with a little tweaking (making sure you don’t echo the opening and closing ul tags until you’ve made sure the post has at least one PDF attachment), you could just place the code shown above in your template file, and the list would automatically be generated for any post or page that includes PDF attachments.
As a caveat, this type of modification needs to be made to the theme files rather than to the post or page itself. So, if you don’t have access to modify the theme files, this solution will not work for you.
Post Your Comment