2017-05-03 139 views
3

我的代码如下,检查是否Wordpress成员是男性还是女性,并基于此显示某些代码。我试图优化下面的代码,以避免必须有2个整个代码块的副本,因为在我看来,我只需要有条件地检查代码的第一个ACF,因为这是指性别特定的内容?我怎样才能做到这一点?多个PHP if语句

下面的当前代码工作正常,但会导致大量重复的代码。下面的尝试不起作用,它似乎与<? endif; ?>标签混淆?

CURRENT

<?php if ($memberGender == "male") : ?> 
<section> 
    <?php if(have_rows('accordion_section_boys')): ?> 
    <?php while(have_rows('accordion_section_boys')): the_row(); ?> 

    <div class="accordion-section"> 
     BOY SPECIFIC CONTENT 
    </div> 
    <?php endwhile; ?> 
    <?php endif; ?> 
</section> 
<?php endif; ?> 

<?php if ($memberGender == "female") : ?> 
<section> 
    <?php if(have_rows('accordion_section_boys')): ?> 
    <?php while(have_rows('accordion_section_boys')): the_row(); ?> 

    <div class="accordion-section"> 
     GIRL SPECIFIC CONTENT 
    </div> 
    <?php endwhile; ?> 
    <?php endif; ?> 
</section> 
<?php endif; ?> 

未遂

<section> 
    <?php if ($memberGender == "male") : ?> 
     <?php if(have_rows('accordion_section_boys')): ?> 
     <?php while(have_rows('accordion_section_boys')): the_row(); ?> 
    <?php endif; ?> 

    <?php if ($memberGender == "female") : ?> 
     <?php if(have_rows('accordion_section_girls')): ?> 
     <?php while(have_rows('accordion_section_girls')): the_row(); ?> 
    <?php endif; ?> 

    <div class="accordion-section"> 
     GENDER SPECIFIC CONTENT (BOY OR GIRL) 
    </div> 
    <?php endwhile; ?> 
    <?php endif; ?> 
</section> 
<?php endif; ?> 
+0

你尝试只有一个'endwhile',所以这是不是一个好兆头。 –

+0

你的新代码会得到什么结果? – Scriptman

+0

在两节中都有两个ifs,然后是一个endif。还有两段时间,只有一段时间。所以你总是会得到男性内容,但从来没有女性内容。 – aynber

回答

1
<section> 
    <?php if ($memberGender == "male") : ?> 
     <?php $val = 'accordion_section_boys';?> 
    <?php endif; ?> 
    <?php if ($memberGender == "female") : ?> 
     <?php $val = 'accordion_section_girls';?> 
    <?php endif; ?> 
    <?php if(have_rows($val)): ?> 
    <?php while(have_rows($val)): the_row(); ?> 

     <div class="accordion-section"> 
      BOY SPECIFIC CONTENT 
     </div> 
    <?php endwhile; ?> 
    <?php endif; ?> 
<section> 
+1

完美 - 谢谢!我已经标记这是正确的答案,[请随时upvote问题:) –

+0

@ dungey_140最好使用其他的第二个如果。如果'$ memberGender'不等于男性和女性,这个答案可能会导致问题。另外为什么你需要打开和关闭这个简单的东西?在这种情况下,这是没有必要的。它使您的代码难以阅读并且难以调试。 – ICE

0

我建议是这样的:

<?php 

$genders = array(
    'male' => 'accordion_section_boys', 
    'female' => 'accordion_section_girls', 
); 

foreach ($genders as $gender => $rows_id) { 

     while(have_rows($rows_id)) { 

      // Here use a template to print the content, by name them like "template-male" and "template-female" 
      include 'template-' . $gender . '.php'; 

     } 

} 

?> 

如果您发现该代码,我告诉你使用模板显示HTML,所以你c一个动态的给他们打电话,内容将是:

<section> 
    <div class="accordion-section"> 
     CONTENT 
    </div> 
</section> 
0

因为它们具有相同的结构,你可以做这样的事情:

<?php 

    if ($memberGender == 'male' || $memberGender == 'female'){ 

     $indicator = ($memberGender == 'male')? 'boys' : 'girls'; 

     if(have_rows('accordion_section_'.$indicator)){ 
      while(have_rows('accordion_section_'.$indicator)){ 
       the_row(); 
      } 
     } 
    } 
?>