0

I have the following code on my ecommerce product description tabs so that I can enter in all the information one field at a time without having to preformat my product descriptions in Excel with HTML.

<?php echo the_field('color');?><br>
<?php echo the_field('product_info');?><br>
<?php echo the_field('product_size');?><br>
<?php echo the_field('product_size2');?><br>
<?php echo the_field('product_size3');?><br>

Unfortunately this isn't helpful because the
tags outside of the PHP will leave blank lines in cases when no value exists for these fields. How can I incorporate the
tags inside the PHP so that they're used only when a value exists?

I'm clearly new to PHP (and coding in general), so I really appreciate anyone's help. Thank you!

3
  • Are you trying to have like an array with all the information or something? If so, why not use an array? You can parse the array and all the empty values will not be displayed Commented Apr 10, 2014 at 11:01
  • You can use if(isset()) or if(!empty()) to check if they are set or not empty. nl1.php.net/isset and nl1.php.net/empty Commented Apr 10, 2014 at 11:01
  • <?php echo (empty(the_field('product_info')) ? '' : the_field('product_info').'<br/>'); ?> Commented Apr 10, 2014 at 11:01

4 Answers 4

2

Try:

if(get_field('field_name') != "") { echo '<p>' . get_field('field_name') . '</p>'; }

Should work for you

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

2 Comments

or !empty(get_field('field_name'))
That is a much cleaner way! I must use that next time
0

Can you try this:

<?php echo empty(trim(the_field('color'))) ? '' : the_field('color') . '<br>'; ?>

And repeat the same pattern for every field.

Comments

0

Without knowing what your the_field function does and how "heavy" it is, this should avoid unnecessary duplication of the calls to both test and output the result.

<?php if($color = the_field('color')): ?>
    <?php echo $color; ?><br>
<?php endif; ?>
<?php if($product_info = the_field('product_info')): ?>
    <?php echo $product_info; ?><br>
<?php endif; ?>
<?php if($product_size = the_field('product_size')): ?>
    <?php echo $product_size; ?><br>
<?php endif; ?>
<?php if($product_size2 = the_field('product_size2')): ?>
    <?php echo $product_size2; ?><br>
<?php endif; ?>
<?php if($product_size3 = the_field('product_size3')): ?>
    <?php echo $product_size3; ?><br>
<?php endif; ?>

1 Comment

Thanks Brendan! This is great except the <br> tags don't seem to be having any effect no matter how many I use. Is this correct?
-1

Edit: Use this:

<?php echo (the_field('color') != '') ? the_field('color') . '<br>' : ''; ?>
<?php echo (the_field('product_info') != '') ? the_field('product_info') . '<br>' : ''; ?>
<?php echo (the_field('product_size') != '') ? the_field('product_size') . '<br>' : ''; ?>
<?php echo (the_field('product_size2') != '') ? the_field('product_size2') . '<br>' : ''; ?>
<?php echo (the_field('product_size3') != '') ? the_field('product_size3') . '<br>' : ''; ?>

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.