Home › Forums › General Issues › Outputting info from a 'File' field?
Pretty simple what I am trying to do here. I have a ‘File’ field setup on the backend so someone can upload a PDF to my post. I’m able to output the URL of the file in my themes template file using this code:
<?php if( get_field('product_brochure') ): ?>
<a href="<?php the_field('product_brochure'); ?>" >Download File</a>
<?php endif; ?>
However, I also want to be able to output the title of the file as well. How can this be achieved?
This code below seems to work:
<?php
$file = get_field('product_brochure_pdf');
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
endif; ?>
<?php if(get_field('product_brochure') == 'yes') : ?>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank"><?php echo $title; ?></a>
<?php else : ?>
<?php endif; ?>
Can any of you guys who are more versed in PHP check that out and see if there are any glaring issues? Like I said, it does work, just want to make sure it’s coded correctly.
What it should be doing is if someone chooses ‘Yes’ from the radio button choice (meaning yes, there is a product brochure) it will then show the conditional ‘File’ field called Product Brochure PDF (to allow someone to upload a PDF). The code will output the link to the PDF along with the title of the PDF.
If someone selects ‘NO’ on the radio button choice, it simply won’t display anything on the front end.
Yes, it will work, but you should check the radio field first because it doesn’t make sense to get the values for the file unless you’re actually going to show them.
<?php
if (get_field('product_brochure') == 'yes') {
$file = get_field('product_brochure_pdf');
if ($file) {
?>
<a target="_blank" href="<?php
echo $file['url']; ?>" title="<?php
echo $file['title']; ?>"><?php
echo $file['title']; ?></a>
<?php
} // end if file
} // end if product_brochure
Thanks for that! My code worked, but clearly yours is the cleaner and more appropriate version so I went ahead and updated mine!
The topic ‘Outputting info from a 'File' field?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.