- This topic has 3 replies, 2 voices, and was last updated 5 years, 9 months ago by
Steve.
-
AuthorPosts
-
-
May 1, 2016 at 12:45 pm #6406
bradrhMemberHello, Piklist seems really helpful in numerous ways I’ve been needing, but I’m new to WP and PHP and I’ve been having trouble accessing my fields on the front end of my site. I’m trying to create a “download” button so a user can download a pdf that I upload using the Media Uploader.
Here’s my meta-box:
<?php /* Title: Please upload a downloadable Handout Post Type: handout */ // Media Uploader piklist('field', array( 'type' => 'file' ,'field' => 'upload_media' ,'label' => __('Add a .pdf') ,'options' => array( 'modal_title' => __('Add File') ,'button' => __('Add') ) )); ?>and here’s what I’m trying in my template:
<a href=" <?php the_permalink();?>/<?php get_post( $post->ID, 'upload_media', true ); ?> " class="button">Download Handout</a>This just goes to the post, using single.php.
and:
<a href=" <?php $myupload = get_post( $post->ID, 'upload_media', true ); $link = $myupload->upload_media; echo 'link:' . $link; print_r($myupload); ?> " class="button">Download Handout</a>This prints out all that meta data in the browser’s address bar.
I saw somewhere in the User Guide maybe I could use
wp_get_attachment_metadata( $attachment_id, $unfiltered );but I don’t know how to use it.Thanks for any advice!
-
May 1, 2016 at 9:37 pm #6407
SteveKeymaster@bradrh– Welcome to the Piklist community!
I think you’re having issues because of the way WordPress handles file uploads. WordPress creates an “attachment” (a type of Post), for each file uploaded.
Try this tutorial. It might help.
-
May 2, 2016 at 4:48 pm #6408
bradrhMemberHi Steve, Thank you and thanks for the response.
I was referring to that tutorial before. That’s where I got my second code block. But it refers to images and I’m having trouble implementing it. I don’t want to show the doc, just add it’s URL to the anchor’s href.
The bottom part of the tutorial mentions the URL:
“To retrieve the file data you can use the standard WordPressfunction wp_get_attachment_metadata. This function will give you the width, height, file path, sizes, image_meta (aperture, camera, etc) of the uploaded file.”Could you show me what that code would look like, grabbing the file path, and adding it to a wordpress template?
Thank you for your time!
-
May 2, 2016 at 8:57 pm #6410
SteveKeymasterTry this (untested):
$metadata = wp_get_attachment_metadata($post->ID); $file = $metadata['file']; $url = site_url() . $file; echo '< img src="' . $url . '" />';
-
-
AuthorPosts
- You must be logged in to reply to this topic.