Skip to content

Instantly share code, notes, and snippets.

@benhuson
Created November 12, 2012 10:24
Show Gist options
  • Save benhuson/4058529 to your computer and use it in GitHub Desktop.
Save benhuson/4058529 to your computer and use it in GitHub Desktop.
WordPress: Display PDF and Word attachments
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf,application/msword',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$attachments = get_posts( $args );
if ( $attachments ) {
echo '<ul class="attachment-links">';
foreach ( $attachments as $attachment ) {
$mime = '';
switch ( $attachment->post_mime_type ) {
case 'application/pdf':
$mime = '.pdf';
break;
case 'application/msword':
$mime = '.doc';
break;
}
if ( ! empty( $mime ) ) {
$mime = ' (' . $mime . ')';
}
echo '<li><a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank" title="' . $attachment->post_excerpt . '">';
echo $attachment->post_title . $mime;
echo '</a></li>';
}
echo '</ul>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment