如果div有内容的条件,那么就这样做

时间:2013-09-04 11:22:23

标签: php wordpress

我这里有一个剧本

<div>
<?php 
echo '<strong>Other information</strong><br />';
$myname = get_post_meta($post->ID, 'acidity_gl', true); if ( $myname ) { echo 'Acidity: ' . $myname . '<br />'; }
$myname = get_post_meta($post->ID, 'winePh', true); if ( $myname ) { echo 'Wine PH: ' . $myname . '<br />'; }
$myname = get_post_meta($post->ID, 'residual_sugar_gl', true); if ( $myname ) { echo 'Residual Sugar gl: ' . $myname . '<br />'; }
?>
</div>

我想添加条件,如果$ myname有一个值然后显示DIV但是当这3 $ myname没有值时则不显示DIV

3 个答案:

答案 0 :(得分:2)

试试这个,

<?php
    $myname=array();
    $name = get_post_meta($post->ID, 'acidity_gl', true);
    if ( !empty($name) ) { 
        $myname[] = 'Acidity: ' . $name ; 
    }
    $name = get_post_meta($post->ID, 'winePh', true);
    if ( !empty($name) ) { 
        $myname[] = 'Wine PH: ' . $name ; 
    }
    $name = get_post_meta($post->ID, 'residual_sugar_gl', true);
    if ( !empty($name) ) { 
        $myname[] = 'Residual Sugar gl: ' . $name; 
    }
    if(!empty($myname))
    {
        echo '<div>';
        echo '<strong>Other information</strong><br />';
        echo implode('<br />',$myname);
        echo '</div>';
    }
?>

答案 1 :(得分:1)

if(!empty ( $myname ))
{
// write your code for DIV here
}

答案 2 :(得分:0)

使用此代码:

<?php
if(!empty($my_name)){
?>
<div>DIV Content here</div>
<?php
}
?>

或者

<?php
if(!empty($my_name)){
 echo '<div>DIV Content here</div>';
}
?>