Home Forums Add-ons Repeater Field If sub field has value 'x' then do this…


If sub field has value 'x' then do this…

  • Hi

    I’m displaying a list of staff members on a page. I’m using a repeater field with 4 sub fields to store the data. (Photo, Name, Description, Level)

    This is my code which works fine…

    	while( the_repeater_field(thp_coach) ) { ?>
    	<li class="thp-member">
         	<img src="<?php the_sub_field('photo'); ?>">
            <h4><?php the_sub_field('name'); ?></h4>
            <p><?php the_sub_field('description'); ?></p>
         <?php				}

    The 4th sub field contains a dropdown list with three values (Bronze, Silver and Gold)

    I need to apply class to the <li> element depending on the value selected in the sub field ‘level’

    Any idea how to do this?

    I guess I need some kind of ‘if statement’

    If the sub_field contains bronze then do this….

    Thanks in advance


  • Hi Brad, if the values in the “description” dropdown already match your CSS classes (for ease, it would be good if they did), then can you just do something like this:

    while( the_repeater_field(thp_coach) ) {
    <li class=”thp-member <?php the_sub_field('description'); ?>”>
    <img src=”<?php the_sub_field('photo'); ? />”>
    <h4><?php the_sub_field('name'); ?></h4>
    <p><?php the_sub_field('description'); ?></p>
    <?php } ?>

    Basically just adding the “description” dropdown value as a class to the <li>.

    You’d then just end up with something like:

    <li class="thp-member gold"></li>

    No ‘if’ statement required πŸ™‚

  • wickywills – YOU ARE A LEGEND!

    Sometimes it’s the simple things in life eh?

    Thank you so much πŸ™‚

  • No problem πŸ™‚

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘If sub field has value 'x' then do this…’ is closed to new replies.