3

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; ?>
dungey_140
  • 2,602
  • 7
  • 34
  • 68

3 Answers3

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>
Rajaguru R
  • 96
  • 1
  • 8
  • 1
    Perfect - thank you! I've marked this as the correct answer, [please feel free to upvote the question :) – dungey_140 May 03 '17 at 13:25
  • @dungey_140 It's better to use else for that 2nd if. and this answer can cause problem if `$memberGender` not equal to male and female. Also why you need to open and close ` – ICE May 03 '17 at 19:07
0

I would suggest something like:

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

        }

}

?>

If you notice the code, I told you to use a template to show the HTML, so you can call them dynamically and the content will be:

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

Because they have the same structure you can do something like this:

<?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();
            }
        }
    }
?>
ICE
  • 1,667
  • 2
  • 21
  • 43