I am able to parse the data through the variable no problem, but the HTML output for my echo is not being wrapped properly.
<?
if( get_field('pre_video_set_label_name') ) {
echo "<h3>" . the_field('pre_video_set_label_name') . "</h3>";
} else {
echo "<h3>Post-Event Video</h3>";
}
?>
If my input for pre_video_set_label_name
is "Test" then the HTML output becomes:
Test<h3></h3>
My expected output would be:
<h3>Test</h3>
But I'm not getting these results.
Nothing seems to wrap, and I've been having this problem a lot lately. Is there an error in my way of thinking with this?
When you use wordpress / (ACF) functions, always check if they display or return the value.
Function, which displays the value:
If you want to call this function, you won't need a
echo
to display the data, just call it, e.g.Note: The function, won't return the data. But even if it doesn't have an explicit return statement and also won't return the data, it still will return something (NULL).
Function, which returns the value:
If you want to call this function, you will need a
echo
to display the data, just call it, e.g.Note: This function will return the data and doesn't display it by its own.
Different behaviour
You will notice some differences, when you use functions which display or return a value.
Assignment
1.1 Function, which displays the value:
Note:
$variable
, will be assigned withNULL
and the line above will outputdata
.1.2 Function, which returns the value:
Note:
$variable
, will be assigned withdata
and the line above won't output anything.Concatenation
2.1 Function, which displays the value:
Note: You will concatenate
NULL
here, since this function will return this value. The function will displaydata
first, before you see the concatenated string.2.2 Function, which returns the value:
Note: You will concatenate
data
here, since this function will return this value. The function won't display anything first, before you see the concatenated string.Printing
3.1 Function, which displays the value:
Note: This code will output
data
.3.2 Function, which returns the value:
Note: This code won't display anything.
So in your current example you use
the_field()
, which displays the data. But if you want to concatenate it, you will need the data returned, means useget_filed()
, which simply will return the data.There is also a easy way to check what a function returns. Just do:
var_dump(functionCall());
and you will see what the function returns.You should be using
get_field()
withecho
, sincethe_field()
already echoes the meta field:get_field()
returns the meta value, rather than echoing it.