0

Why does the br html tag is ignored in the browser?

<p>
                <?php 
                $footer_1 = the_field('footer_1');
                $footer_2 = the_field('footer_2');
                $footer_3 = the_field('footer_3');
                if (!empty($footer_1)) {
                    the_field('footer_1');
                    echo "<br />";
                }
                if (!empty($footer_2)) {
                    the_field('footer_2');
                    echo "<br />";
                }
                if (!empty($footer_3)) {
                    the_field('footer_3');
                }
                ?>
            </p>

Edit: The browser code outputs the p element as one piece of text. No br tag displayed there either. The three variables are text fields from Advanced Custom Fields.

5
  • What type of custom fields are footer_1, footer_2 and footer_3? Can you show what your browser code inspector shows for the entire paragraph? Commented Apr 27, 2020 at 20:36
  • See above. Thank you. Commented Apr 27, 2020 at 22:09
  • Does this answer your question? Can't assign the value of the_field() to a variable Commented Apr 27, 2020 at 22:11
  • Not really as the_field is displaying the value correctly. It's just the br tag that is not being rendered. Commented Apr 27, 2020 at 22:46
  • You can point people to the exact, precise answer/solution to their problem here, and they still go “nah, doesn’t help” … Commented Apr 28, 2020 at 8:13

1 Answer 1

1

the_field is used to actually echo out the custom field data, so you cannot assign it to a variable. Use get_field instead, like so:

<p>
<?php 
$footer_1 = get_field('footer_1');
$footer_2 = get_field('footer_2');
$footer_3 = get_field('footer_3');
if (!empty($footer_1)) {
    echo $footer_1 . '<br>';
}
if (!empty($footer_2)) {
    echo $footer_2 . '<br>';
}
if (!empty($footer_3)) {
    echo $footer_3;
}
?>
</p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.