WordPress.org

Ready to get started?Download WordPress

Forums

a custom function for displaying attachment files (2 posts)

  1. danielkleindesign
    Member
    Posted 2 years ago #

    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.

  2. marinftw
    Member
    Posted 2 years ago #

    Hi Daniel,
    May be this is posted too late but I think it will help someone any day,
    You are displaying the images from the entire site because you are not receiving the post as parameter on your function, so it does something weird and grab the latest images that you upload on the site, just send the pust as parameter and it will work.

    Ex:

    function get_all_images($post){
    	$args = array(
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'post_status' => 'inherit',
    		'post_mime_type' => 'image',
    		'post_parent' => $post->ID
    	);
    	$attachments = get_posts($args);
    	if ($attachments) {
    		foreach ($attachments as $attachment) {
    			$link=wp_get_attachment_url($attachment->ID);
    			echo $out="<a rel='fBox' href=".$link.">";
    				echo "<img src=".$link.">";
    			echo "</a>";
    
    		}
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic