Question Details

No question body available.

Tags

php wordpress advanced-custom-fields

Answers (3)

Accepted Answer Available
Accepted Answer
November 29, 2025 Score: 4 Rep: 30,476 Quality: High Completeness: 60%

thefield is the same as echo getfield(), so you should use getfield, and you also should escape the value before using it:

$downloadLink = getfield('article-file'); if($downloadLink) { $downloadLink = escattr($downloadLink); .... }
November 29, 2025 Score: 2 Rep: 521 Quality: Low Completeness: 50%

Root cause is you used $downloadLink = thefield('article-file');

But thefield() echo's the value and returns NOTHING (returns null).

That is why $downloadLink becomes empty, so your is empty.

The solution is use getfield() instead — it returns the value so you can store it in a variable.

Replace this: $downloadLink = thefield('article-file'); With this - $downloadLink = get_field('article-file');

November 29, 2025 Score: 0 Rep: 80,854 Quality: Medium Completeness: 80%

thefield is described as follows:

Intuitive and powerful, this function can be used to output the value of any field from any location. Please note this function is the same as echo getfield().

Emphasis from me. Hence, your

$downloadLink = thefield('article-file');

receives whatever thefield returns (I did not find the code) and that's evaluated when you use it as an empty string, this is why your href ends up being empty. However, look at your function. It returns a template. You don't want to echo anything when you call the function, because that would echo during the processing. Which is definitely not what you want. Instead, you need:

$downloadLink = getfield('article-file');

And then, whatever your article-file field is will be assigned as a value to $downloadLink and you can concatenate it into your template like you did.