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; ?>
3
  • Your attempt only has one endwhile, so that's not a good sign. Commented May 3, 2017 at 12:32
  • And what result do you get with your new code? Commented May 3, 2017 at 12:32
  • You have two ifs, then one endif, in both sections. And two whiles with only one endwhile. So you'll always get the male content, but never the female content. Commented May 3, 2017 at 12:36

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect - thank you! I've marked this as the correct answer, [please feel free to upvote the question :)
@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 <?php for this simple thing? in this case it isn't necessary. It makes your codes hard to read and hard to debug.
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>

Comments

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();
            }
        }
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.