Problems with displaying file uploads…

This topic contains 9 reply and 2 voices, and was last updated by paperweight 11 years, 5 months ago
Viewing 9 Posts - 1 through 9 (of 9 total)
Author Posts
June 2, 2013 at 1:33 am 4416
paperweight I have 2 problems with file upload displays: 1) I am trying to show the file upload links on the Post page. I followed the instructions and added "file_uploads" to the code here, but it still does not show on my Post pages. Any idea why?
<?php
$images = get_post_meta( $post->ID, 'file_uploads' );
 
if ( $images ) {
    foreach ( $images as $attachment_id ) {
        $thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' );
        $full_size = wp_get_attachment_url( $attachment_id );
 
        printf( '<a href="%s">%s</a>', $full_size, $thumb );
    }
}
?>
2) Also, I would like to have a small icon of a PDF or DOC next to each download link so the user knows if it is a PDF or DOC file. Is there a way to check the filetype and output a different pdf.jpeg or doc.jpeg?
June 2, 2013 at 11:15 am 4429
Tareq Hasan Tareq Hasan

1. Check if you are getting the posts ID by using $post->ID, there might be some plugin or your theme can cause problems.

2. As attachment ID’s are stored for each upload and you get the attachment ID in that foreach loop, you can check the mime type in this way.

June 2, 2013 at 3:21 pm 4432
paperweight paperweight

1) I also have images uploaded for a Post, and that code above works fine when using the correct mey_key_name for the image field. However for the file uploads they are not showing. When you say I should use $post->ID you mean what? That seems to already be in the code, right?

2) Ah, great thanks~

June 2, 2013 at 5:36 pm 4435
Tareq Hasan Tareq Hasan

$post->ID is the current posts post ID. so if you get the post id correctly (<?php echo $post->ID; ?>), that means it should work fine. As you’ve said you are getting images correctly, that means files should work correctly too. They work in exact same way. Do you see the meta key values in the admin area correctly? If yes, I don’t see any reason why it shouldn’t work.

June 2, 2013 at 7:07 pm 4441
paperweight paperweight

Ok, I got it working 50%. I changed the code to the following:

<?php
$files = get_post_meta( $post->ID, 'file_uploads' );
if ( $images ) {
    foreach ( $files as $attachment_id ) {
        $thumb = wp_get_attachment_link( $id, 'array(32,32)' , false, false, 'My link text' );
        $full_size = wp_get_attachment_url( $attachment_id );
 
        printf( '<a href="%s">%s</a>', $full_size, $thumb );
    }
  }
?>

Most importantly, I changed the wp_get_attachment_image to wp_get_attachment_link, so there are now more arguments that need to be added.

However, the “My link text” does not display. Instead, it says “Missing Attachment” and then a link to the correct attachment.

So, it is now correctly pointing to the correct attachment, but the linked text is incorrect. Any idea why it is saying “Missing Attachment”?

June 2, 2013 at 7:26 pm 4442
Tareq Hasan Tareq Hasan

I am not sure, but the quote around array(32,32) will not be there for sure.

June 2, 2013 at 11:33 pm 4451
paperweight paperweight

Yes, I removed and added the quotes around that a few times with other changes too but the Missing Attachment text always shows.

WordPress says here http://codex.wordpress.org/Function_Reference/the_attachment_link that the Missing Attachment will display if an attachment can’t be found…. but I do have an attachment and that Missing Attachment text accurately links to that file. Very weird and I’m unclear how to fix this…

June 3, 2013 at 4:46 pm 4473
Tareq Hasan Tareq Hasan

So it’s actually displaying the image, but it’s showing the missing attachment text? Are you sure $files = get_post_meta( $post->ID, 'file_uploads' ); returns any file list? Try printing it like this: print_r($files); and see if the upload ids are there.

June 3, 2013 at 5:04 pm 4478
paperweight paperweight

I have found a 75% solution and still working on it. I will post to this thread as soon as I get closer to 100% 😉

June 4, 2013 at 12:47 am 4524
paperweight paperweight

ok so here is the code I am using now. It is about 85% to where I need it to be, but I can;t figure out anymore:

<?php  if((get_post_meta( $post->ID, 'file_uploads', true ))) { ?>
<?php
$files = get_post_meta( $post->ID, 'file_uploads' );
if ( $files ) {
    foreach ( $files as $attachment_id ) {
        $thumb = wp_get_attachment_link( $id, array(32,32) , false, true, 'My link text' );
        $full_size = wp_get_attachment_url( $attachment_id );
 
        printf( '<a href="%s"><strong>DOWNLOAD FILE</strong></a>', $full_size, $thumb );
    }
  }
?>
<?php } ?>

The array(32,32) seems useless so far and I can’t get it to display an icon to display a PDF image if the attachment is a PDF or a doc image if attachment is doc, etc. But the good news is it does display correctly the text “DOWNLOAD FILE” with the correct link to the attached file 🙂

I have a feeling some of my code above is useless, but it sort of works so far~

Viewing 9 Posts - 1 through 9 (of 9 total)