Support

Account

Home Forums General Issues Select (value) conditional statement in repeater

Helping

Select (value) conditional statement in repeater

  • I can’t for the life of me get this to work. Please help if you can.

    I have a repeater, which has a select (otaname). I want to show an image based on the set value of the select field for each row of the repeater. The image just isn’t working – it defaults to the ‘else’ because it won’t recognise one being selected.

     <?php if ( have_rows( 'ota_links' ) ): ?>
                                    <ul class="menu horizontal" id="otas-menu">
       <?php
    		                            while ( have_rows( 'ota_links' ) ) : the_row();
    			                            $otaname = get_sub_field( 'otaname' );
    			                            $otalinkurl  = get_sub_field( 'ota_link_url' ); ?>
    
       <li><a href="<?php echo $otalinkurl ?>" target="_blank"><span data-tooltip aria-haspopup="true" class="has-tip" title="<?php echo esc_attr( $ota['value'] ); ?>">
    				                            <?php if ( $ota == 'expedia' ) { ?><img
                                                    src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/ota_expedia.png" height="45" width="45" alt="<?php echo esc_attr( $ota['label'] ); ?>"/>
    				                            <?php } elseif ( $ota == 'hotels' ) {  ?><img
                                                    src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/ota_hotelcom.png" height="45" width="45" alt="<?php echo esc_attr( $ota['label'] ); ?>"/>
    				                            <?php } elseif ( $ota == 'agoda' ) {  ?><img
                                                    src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/ota_agoda.png" height="45" width="45" alt="<?php echo esc_attr( $ota['label'] ); ?>"/>
    				                            <?php } elseif ( $ota == 'wotif' ){  ?><img
                                                    src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/ota_wotif.png" height="45" width="45" alt="<?php echo esc_attr( $ota['label'] ); ?>"/>
    				                            <?php } elseif ( $ota == 'airbnb' ) {  ?><img
                                                    src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/ota_airbnb.png" height="45" width="45" alt="<?php echo esc_attr( $ota['label'] ); ?>"/>
    				                            <?php } else { ?><img src="<?php bloginfo( 'stylesheet_directory' ); ?>/images/ota_tripadvisor.png" height="45" width="45" alt="<?php echo esc_attr( $ota['label'] ); ?>"/>
    				                            <?php } ; ?></span></a> </li>
    
                                    </ul>
                               
    						<?php endif; ?>
  • Looks like you’re declaring and assigning a value to $otaname on row 5 in your snippet. But then you never actually use that variable?

    Also. You use the variable $ota in your series of if-statements. The way your code is written, you’re assuming that it has a string value. But on each of the conditional statement blocks, you also output $ota[‘label’], assuming $ota is an array.

    My best guess is that you’ve mixed up $ota and $otaname in your code.

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

The topic ‘Select (value) conditional statement in repeater’ is closed to new replies.