多个PHP if语句

时间:2017-05-03 12:30:10

标签: php wordpress advanced-custom-fields

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

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

当前

<?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; ?>

ATTEMPT

<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; ?>

3 个答案:

答案 0 :(得分: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 :(得分:0)

我会建议像:

string

如果您注意到代码,我告诉您使用模板显示HTML,这样您就可以动态调用它们,内容将是:

raw_data = raw_data.withColumn("LATITUDE_ROUND", format_number(raw_data.LATITUDE, 3))

答案 2 :(得分: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();
            }
        }
    }
?>
相关问题