1
votes

I created a File Custom Field in Wordpress using ACF plugin called: pdf

Now I need to print this field in the frontend.

I'm almost there using this line of code in my function.php file

echo the_field('pdf'); 

Problem is that I get several values printed:

3491, , audiometria-1, , , application/pdf, http://example/wp-content/uploads/2017/02/audiometria-1.pdf

I guess this an object or array since it is comma separated.

I need to echo just the last value which the url of the file.

What am I missing here?

I searched in the documentation page for this plugin but I see no way to add parameters to the_field function: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

2

2 Answers

2
votes

If you just want to output the file URL onto the page (or into like an anchor, 'href') then you'll need to get that part of the array using something to the effect of:

$file = get_field('pdf');
echo $file['file'];

However, that's if you're not using the ACF plugin. In the ACF plugin it let's you specify the output type of the field. Go to the custom field settings and change the return value to File URL.

ACF plugin UI for File fields

Save it, then all you need to do is:

echo the_field('pdf');

You can read the full documentation about it here.

0
votes

It is a string not an array and you could extract the link using something like:

$fdata = explode(",", the_field('pdf'));
$link = array_pop($fdata);
echo $link;