a custom function for displaying attachment files
-
Hi all, I’m not well-versed in php and really need some quick advice in fine-tuning this function I have for my site. The idea is, on single posts, to display a nice row of attachment files with custom icons assigned to each mime-type. I found some great code to add to my Functions.php file, which does a good job making this work on all single posts. Now I need to add a similar function that will show THE SAME attachments on my Image.php and Attachment.php templates (because I want it to look as if the user is still on the same page whenever they are navigating through all the images associated with a post).
So here is the function I am using on my Single Post template:
//icons for attachments function get_attachment_icons($echo = false){ //PDF if ( $files = get_children(array( //do only if there are attachments of these qualifications 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'application/pdf', //MIME Type condition ))){ foreach( $files as $file ){ //setup array for more than one file attachment $file_link = wp_get_attachment_url($file->ID); //get the url for linkage $file_name_array=explode("/",$file_link); $file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename $sAttachmentString .= "<dt class='icon'>"; $sAttachmentString .= "<a href='$file_link'>"; $sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/File-Extension-Pdf.png'/ width='16' height='16'>"; $sAttachmentString .= "</a>"; $sAttachmentString .= "<dd class='caption'>"; $sAttachmentString .= "<a href='$file_link'>Datasheet</a>"; $sAttachmentString .= "</dd>"; $sAttachmentString .= "</dt>"; } }
This repeats for each file type that I want to show an icon.
Now I am writing a function based on this same principle, but I need it to use the PARENT POST ID so that it will show the same attachments as the above function when used on my Image template. I have tried many alterations of this code, but the configuration you see below is actually displaying all of the attachments for the entire site, and not filtering out the specific parent id that I need it to. This is what I have right now:
//icons for attachments of parent function get_sibling_icons(){ //PDF if ( $files = get_posts(array( //do only if there are attachments of these qualifications 'id' => post_parent, 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'application/pdf', //MIME Type condition ))){ foreach( $files as $file ){ //setup array for more than one file attachment $file_link = wp_get_attachment_url($file->ID); //get the url for linkage $file_name_array=explode("/",$file_link); $file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename $sAttachmentString .= "<dt class='icon'>"; $sAttachmentString .= "<a href='$file_link'>"; $sAttachmentString .= "<img src='".get_bloginfo('template_directory')."/images/File-Extension-Pdf.png'/ width='16' height='16'>"; $sAttachmentString .= "</a>"; $sAttachmentString .= "<dd class='caption'>"; $sAttachmentString .= "<a href='$file_link'>Datasheet</a>"; $sAttachmentString .= "</dd>"; $sAttachmentString .= "</dt>"; } }
Someone please help me figure this out—I feel like I am almost there, but probably making a silly mistake that I am not advanced enough to notice.
- The topic ‘a custom function for displaying attachment files’ is closed to new replies.