Support

Account

Home Forums Add-ons Repeater Field Problem with getting value from repeater sub field in conditional

Solved

Problem with getting value from repeater sub field in conditional

  • I’m trying to use the content in a subfield in a repeater to check against the url of my site to select a different template part, but I keep getting stuck in a loop and I can’t figure out what I’m doing wrong.

    <?php 
    
    $uri = $_SERVER['REQUEST_URI']; 
    //repeater  
    if(have_rows('new_banner') ):
        while( have_rows('new_banner') ) : the_row();
      //get subfield value to check against the URI
          if($uri == get_sub_field('full_value')){
            echo 'true';
            // If subfield value matches URI select this template part.
       get_template_part('templates/frontpage', 'masthead-alt');
    
    }else{
      // If subfield is not a match or anything else then get the other template part.
    echo 'false';
     get_template_part('templates/frontpage', 'masthead'); 
      }
    
      endwhile;
    endif;
    
    ?>

    Any help?

  • Henry,

    Is this all of your code your using at the top of your template file?

    Jeff

  • I ended up getting this figured out.The fix that worked was having two if statements rather than an if/else statement. I used a simple variable to help with getting the template i needed.

    <?php
    $banners = get_field('new_banner');
    $uri = $_SERVER['REQUEST_URI'];
    $host = $_SERVER['SERVER_NAME'];
    ?>
    
    <?php 
    
    $match = 0;
    
      foreach($banners as $banner){
      	$banner_match = $banner['full_value'];
    
     	if($uri === $banner_match){
    
    		get_template_part('templates/frontpage', 'masthead-alt');
    			$match = 1;
     		}
     	}
     	if($match !== 1) {
    
    		get_template_part('templates/frontpage', 'masthead');
       		}
        
     ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Problem with getting value from repeater sub field in conditional’ is closed to new replies.