Reputation: 11
I am trying to create a list of PDFs (newsletters created each month). I have created a custom post type named 'newsletters' and restricted it to only supporting a 'title'.
I have then used the advanced custom fields plugin to add a file upload button to this post type. Therefore each post has a title and a button to upload the pdf.
I have then written the below function to output the list of attachments.
function list_newsletters(){
$args = array( 'post_type' => 'newsletters' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$permalink = get_permalink();
$title = get_the_title();
$id = get_the_ID();
$attachment = wp_get_attachment_url($id);
echo '<li><a href="'.$attachment.'">'.$title.'</li>';
endwhile;
}
However the wp_get_attachment_url($id) doesn't seem to work. I think this is because I am supposed to be supplying the attachment ID rather than the post ID. I have looked around online and cannot find a clear way of finding the attachment ID for a specific post.
Just to clarify each post will only contain one attached file.
Thank you in advance
Upvotes: 1
Views: 7352
Reputation: 62392
This example taken from the get_posts()
Codex page
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' =>'any',
'post_parent' => $post->ID
));
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
Upvotes: 1