Question

How can I hide a specific field on a page displaying data from a form if no value is submitted? An example is below:

<?php if($price): ?>
<li><?php echo $price;?>: <?php echo get_property_price($post->ID);?>&nbsp;<?php echo get_post_meta($post->ID,'rentperiod',true);?></li>
<?php endif; ?>

I don't want the $price or rentperiod values to display as a list item if they are empty. What's the best way to accomplish this?

Était-ce utile?

La solution

Get the data first, then display it conditionally. Also avoid changing too often between php and html to get more readable code.
Use the variable expansion in double quotes strings.

$property_price = get_property_price($post->ID);
$rentperiod = get_post_meta($post->ID,'rentperiod',true);

if($price && ($property_price || $rentperiod)) 
    echo "<li>$price: $property_price&nbsp;$rentperiod</li>";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top