문제

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