1

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>';
    }
?>
1
  • Not sure why you guys are marking this On Hold...it is WordPress, Advanced Custom Fields and PHP related? Jan 29, 2014 at 15:55

1 Answer 1

4

The problem is that the_sub_field() echoes the Subfield, so in your conditional the field company_name gets echoed.

However, this function does not return true, so the conditional is not met.

You need to use another if statement:

if ( get_sub_field('company_name') && get_sub_field('company_name') != '' ) {

    // echo it

}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.