I'm building a site and each post has a PDF file attachment. Is there a way I can access that file outside the_content()
on a single post page? I'd like to put the file attachment in a sidebar and not in the body of the post.
2 Answers
Try the following code:
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $attachment) {
setup_postdata($attachment);
the_title();
the_attachment_link($attachment->ID, false);
the_excerpt();
}
}
-
Thanks Maxim - When I do that I get a lot of extra text that doesn't seem to be related to the attachment. I took out
the_title()
andthe_excerpt()
but need to figure out formatting. Commented Jun 19, 2012 at 18:04 -
Also would like to figure out how to exclude an attachment from this list -- or better yet set an attachment as "featured" and only display that. Commented Jun 19, 2012 at 18:05
-
I ended up using the WP-Filebase Download plugin. This allowed me to pull the attachments into a shortcode list that I could put in my template file outside
the_content()
. Commented Jun 19, 2012 at 18:29 -
I think I don't understand what you expect to receive. Do you you need a shortcode? or better formatting? or both? Commented Jun 21, 2012 at 8:05
-
I was searching for a plugin that would allow me to set a "featured attachment" and then use a shortcode or some php on the page to allow for that attachment to be downloaded. Sorry I wasn't clear. Commented Jun 21, 2012 at 12:45
I ended up using the WP-Filebase Download plugin. This allowed me to pull the attachments into a shortcode list that I could put in my template file outside `