【问题标题】:How to not display in php if the field is empty如果字段为空,如何在php中不显示
【发布时间】:2018-09-01 07:32:16
【问题描述】:

如果值不存在,任何人都可以帮助如何隐藏字段和标题。例如,如果没有传真号码,它也不应该显示标题名称“传真:”。

<?php
$name = get_sub_field('name');
$address = get_sub_field('address');
$fax = get_sub_field('fax');
?>

<div class="col-md-10">
    <h6><?php echo (isset($name)) ? $name : ''; ?></h6>
    <p>P.O. Box: <?php echo (isset($address)) ? $address : ''; ?></p>
    <p>Fax: <?php echo (isset($fax)) ? $fax : ''; ?></p>
</div>

【问题讨论】:

  • if(!empty($fax)) { //显示传真 }(取决于你的函数的返回值)
  • 如果找不到faxget_sub_field 会返回什么?
  • 将您必须的传真线路更改为:&lt;?php echo (isset($fax) ? '&lt;p&gt;Fax: ' . $fax . '&lt;/p&gt;' : ''); ?&gt;

标签: php function hide


【解决方案1】:
<?php
$name = get_sub_field('name');
$address = get_sub_field('address');
$fax = get_sub_field('fax');
?>

<div class="col-md-10">
<?php
    if(isset($name)){
?>
        <h6><?=$name ?> </h6>
<?php    
    }    
?>
 ....
</div>

这会奏效。不过看起来很乱。

【讨论】:

    【解决方案2】:

    我认为这可以解决问题。

        <?php
        $name = get_sub_field('name');
        $address = get_sub_field('address');
        $fax = get_sub_field('fax');
        ?>
    
        <div class="col-md-10">
        <h6><?php echo (!empty($name)) ? $name : ''; ?></h6>
        <p><?php echo (!empty($address)) ? 'P.O. Box: '.$address : ''; ?></p>
        <p><?php echo (!empty($fax)) ? 'Fax: '.$fax : ''; ?></p>
        </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-13
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多