Using Advanced Custom Fields in WordPress, I have the following code:
if(the_sub_field('company_name')) {
echo '<strong>Company name:</strong>';
echo '<p>' . the_sub_field('company_name') . '</p>';
}
When the page renders though, the company name renders (the_sub_field) but the Company name: does not.
This has also happened in a number of other places in my code when I echo some html out.
Is there a reason for this? Does it have to do with ACF? Many thanks
EDIT:
Even if I try it like this the Company name: is stripped compltely:
<?php if(the_sub_field('company_name')) { ?>
<strong>Company name:</strong>
<p><?php the_sub_field('company_name') ?></p>;
<?php } ?>
EDIT:
Placing the Company Name: outside of the IF statement works, but I would like for it to be inside the IF statement as I want it to render out only if that statement is true of course.
<?php
echo '<strong>Company name:</strong>';
if(the_sub_field('company_name')) {
echo '<p>' . the_sub_field('company_name') . '</p>';
}
?>
The problem is that
the_sub_field()
echoes the Subfield, so in your conditional the fieldcompany_name
gets echoed.However, this function does not return true, so the conditional is not met.
You need to use another
if
statement: