【问题标题】:How can use multi "if" in one tag如何在一个标签中使用多个“if”
【发布时间】:2017-05-21 06:15:54
【问题描述】:

如何在一个标签like <header> 中使用多个“if”。 每个div 都与一个选项卡相关。所以我想为每个选项卡设置一个条件,以便显示。

<?php					
$mainbox = get_field('on');
$guide = get_field('install_guide');
$required = get_field('required');
?>
<section class="fieldset">	
<header>
<?php
if (!empty($mainbox)){
<div class="download-taber active">	tab1</div> //// if true show this otherwise hide it
<div class="attr-taber">tab2</div>} //// if true show this otherwise hide it

elseif (!empty($guide)){	
<div class="install-guide-taber">tab3</div>} //// if true show this otherwise hide it

elseif (!empty($required)){
		<div class="system-required-taber">tab4</div>} //// if true show this otherwise hide it
?>		
</header>
</section>
<?php endif; ?>

【问题讨论】:

  • 您没有正确关闭您的条件标签。应该是这样的&lt;?php if (!empty($mainbox)){ ?&gt; &lt;div class="download-taber active"&gt; tab1&lt;/div&gt; //// if true show this otherwise hide it &lt;div class="attr-taber"&gt;tab2&lt;/div&gt; &lt;?php } ?&gt;
  • 我认为您不需要添加关闭和打开 php 标签 :-)
  • 你不应该使用elseif否则只会显示第一个条件为真的选项卡。

标签: php if-statement advanced-custom-fields


【解决方案1】:

简单(列出语句以便于交换值以进行测试):

<?php                   
list($mainbox, $guide, $required)  = [true, true, true];
?>
<section class="fieldset">  
    <header>
    <?php if($mainbox) { ?>
        <div class="download-taber active">tab1</div>
        <div class="attr-taber">tab2</div>
    <?php } ?>
    <?php if($guide) { ?>
        <div class="install-guide-taber">tab3</div>
    <?php } ?>
    <?php if($required) { ?>
        <div class="system-required-taber">tab4</div>
    <?php } ?>      
    </header>
</section>

如果所有变量都设置为 true,则将显示所有选项卡。

【讨论】:

    【解决方案2】:

    要在 PHP 中嵌入 html,您必须将 html 代码保留在 php 的 echo 标记中 尝试这个。

     <?php                  
        $mainbox = get_field('on');
        $guide = get_field('install_guide');
        $required = get_field('required');
        ?>
        <section class="fieldset">  
        <header>
        <?php
        if (!empty($mainbox)){
        echo "<div class='download-taber active'>tab1</div>"; //// if true show this otherwise hide it
        echo "<div class='attr-taber'>tab2</div>";} //// if true show this otherwise hide it
    
        elseif (!empty($guide)){    
        echo "<div class='install-guide-taber'>tab3</div>";} //// if true show this otherwise hide it
    
        elseif (!empty($required)){
                echo "<div class='system-required-taber'>tab4</div>";} //// if true show this otherwise hide it
        ?>      
        </header>
        </section>
        <?php endif; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2016-10-22
      • 2017-05-15
      相关资源
      最近更新 更多