Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple PHP if statements

My code below is checking to see if a Wordpress member is male or female, and the displaying certain code based on this. I am trying to optimise the code below to avoid having to have 2 copies of the entire code block, as it appears to me that I only need to conditionally check the first piece of ACF if code, as this is referring to the gender specific content? How can I achieve this?

The current code below is working correctly, but results in lots of duplicate code. The attempt below does not work, it appears to be getting confused with the <? endif; ?> tags?

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

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; ?>
like image 891
dungey_140 Avatar asked Mar 12 '26 15:03

dungey_140


1 Answers

<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>
like image 187
Rajaguru R Avatar answered Mar 15 '26 05:03

Rajaguru R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!