我的代码如下,检查是否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; ?>
你尝试只有一个'endwhile',所以这是不是一个好兆头。 –
你的新代码会得到什么结果? – Scriptman
在两节中都有两个ifs,然后是一个endif。还有两段时间,只有一段时间。所以你总是会得到男性内容,但从来没有女性内容。 – aynber