0

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?

4

1 回答 1

0

先获取数据,再有条件地显示。还要避免在 php 和 html 之间频繁更改以获得更具可读性的代码。
在双引号字符串中使用变量扩展。

$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>";
于 2013-10-09T18:04:27.993 回答