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?

有帮助吗?

解决方案

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>";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top