0

I have this piece of code:

<h4>Size: <?php the_field('size'); ?></h4>
<h4>Reference: <?php the_field('reference'); ?></h4>

If there are no values for size and reference, the texts 'Size' and 'Reference' are displayed. So I would like to add the texts "Size" and "Reference" inside the PHP function, and if there are no values on size and reference then "Size" and "Reference" are not displayed.

Sorry, I know it's a basic thing but I am just starting with PHP! Thanks in advance.

1
  • You need an if/else statement inside your PHP Commented Dec 9, 2020 at 20:18

2 Answers 2

2

This is one way to do it:

<?php
if ($size = the_field('size')) {
    echo "<h4>Size: $size</h4>";
}
if ($reference = the_field('reference')) {
    echo "<h4>Reference: $reference</h4>";
}
?>

Another way:

<?php if ($size = the_field('size')):?>
    <h4>Size: <?=$size?></h4>
<?php endif?>
<?php if ($reference = the_field('reference')):?>
    <h4>Reference: <?=$reference?></h4>
<?php endif?>

or if you have short open tags enabled:

<?if ($size = the_field('size')):?>
    <h4>Size: <?=$size?></h4>
<?endif?>
<?if ($reference = the_field('reference')):?>
    <h4>Reference: <?=$reference?></h4>
<?endif?>

All of the above solutions consider 0 and "0" as having no value and so nothing is shown. If you need to show values of 0 you need to do this:

if (($size = (string)the_field('size'))!=="") {
    echo "<h4>Size: $size</h4>";
}
if (($reference = (string)the_field('reference'))!=="") {
    echo "<h4>Reference: $reference</h4>";
}

This will cast 0 to a string and become "0" which when compared to an empty string is not equal. While false and null if cast to a string become an empty string.

Sign up to request clarification or add additional context in comments.

2 Comments

Much appreciated! Examples 1 and 2 fit my needs, however, the HTML does not work. Having size as test and reference as 1234 the result is test1234. It seems that the <h4> tags do not work, because it's displayed as something like <p>test1234</p> instead of <h4>test</h4> <h4>1234</h4>
Try adding <style>h4{display:block !important}</style>
0

Assuming your "no values" means any value that's treated as falsy ("", null, 0, false, [], etc.), then you can do a simple truthy if condition or ternary (<truthy> ? <true> : <false>). You can also move the variable assignments up and grouped so you can have a more organized, cleaner, and easier-to-read code, eg,

<?php
...
$size = the_field('size');
$size = $size ? htmlentities($size) : 'Size';
$reference = the_field('reference');
$reference = $reference ? htmlentities($reference) : 'Reference';
...
?>
...
    <h4>Size: <?php echo $size; ?></h4>
    <h4>Reference: <?php echo $reference; ?></h4>
...

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.