2017-05-03 177 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(); 
      } 
     } 
    } 
?>