Support

Account

Home Forums Add-ons Options Page Select specific repeater rows from an options page

Solving

Select specific repeater rows from an options page

  • Hi I’m setting up an options page called “services”, I have made a field group that contains a repeater made up of service_icon,service_title,service_teaser,service_description

    I then populate the services option page with all the rows of data.

    What I what to be able to do, it on my pages be able to select a specific row to display

    I’m not really sure how to go about this

  • There are a number of ways you can do this. One would be to have some other identifier and then on the page you want to display them on have a choice field where you select from a list of this other identifier to show only the ones that have it. Another way would be to create a select field that lets you choose them by the service title. https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

  • Thanks John

    Could you give any more detail on the second option, I tried following the examples but am struggling!

  • 
    // loading the field
    add_action('acf/load_field/name=select_field_name', 'load_select_field_name_choices');
    function load_select_field_name_choices($field) {
      $choices = array();
      if (have_rows('repeater_field_name', 'options')) {
        while (have_rows('repeater_field_name', 'options')) {
          the_row();
          $title = get_sub_field('services_title');
          $choices[$title] = $title;
        } // end while have rows
      }  // end if get field
    } // end function 
    
    
    // displaying the values
    $selections = get_field('select_field_name');
    // this is assuming that the select field returns an array of selected values
    if (!empty($selections)) {
      if (have_rows('repeater_field_name', 'options')) {
        while (have_rows('repeater_field_name', 'options')) {
          the_row();
          $title = get_sub_field('services_title');
          if (in_array($title, $selections)) {
            // get other sub fields from the repeater and display here
          } // end if selection is selected
        } // end while have rows
      }  // end if get field
    } // end if !empty
    
  • Hi again, I managed to get the select field to dynamically populate, but the I’m not getting and fields to display on the page

  • did you replace this

    
    // get other sub fields from the repeater and display here
    

    with code to display the contents of the fields from the options page?
    I did not include that.

  • Yes my code is below

      <?php  // displaying the values
    $selections = get_field('service_select');
    // this is assuming that the select field returns an array of selected values
    if (!empty($selections)) {
      if (have_rows('services', 'options')) {
        while (have_rows('services', 'options')) {
          the_row();
          $title = get_sub_field('service_title');
          $icon = get_sub_field('service_icon');
    			$title = get_sub_field('service_title');
    			$teaser = get_sub_field('service_teaser');
    			$description = get_sub_field('service_description');
    			$link = get_sub_field('service_link');
          if (in_array($title, $selections)) {
            // get other sub fields from the repeater and display here ?>
            <div class="callout"  data-equalizer-watch="callout">
    					<img  src="<?php echo $icon['url']; ?>" alt="<?php echo $icon['alt'] ?>" />
    						<h5 class="text-center"><?php echo $title ?></h5>
    	
    						
    	
    						<p class="text-center"><?php echo $teaser ?></p>
            </div>
    
      <?php    } // end if selection is selected
        } // end while have rows
      }  // end if get field
    } // end if !empty
          ?>
          
  • Does the field ‘service_select’ allow one value or multiple to be selected?

  • 
    <?php 
      // displaying the values
      $selection = get_field('service_select');
      if (!empty($selections)) {
        if (have_rows('services', 'options')) {
          while (have_rows('services', 'options')) {
            the_row();
            $title = get_sub_field('service_title');
            if (in_array($title == $selection)) {
              // get the other sub fields after checking 
              // instead of getting values that may not be needed
              $icon = get_sub_field('service_icon');
              $title = get_sub_field('service_title');
              $teaser = get_sub_field('service_teaser');
              $description = get_sub_field('service_description');
              $link = get_sub_field('service_link');
              ?>
                <div class="callout"  data-equalizer-watch="callout">
                  <img  src="<?php echo $icon['url']; ?>" alt="<?php echo $icon['alt'] ?>" />
                  <h5 class="text-center"><?php echo $title ?></h5>
                  <p class="text-center"><?php echo $teaser ?></p>
                </div>
              <?php 
            } // end if selection is selected
          } // end while have rows
        }  // end if get field
      } // end if !empty
    ?>
    
  • I’m still not getting anything, though I just tried removing the if statements from your code just to check, and it outputs all the repeater row, I don’t know if that helps narrow down where the problem is

  • ignore me my previous code seems to be working now!

  • Sorry for bringing this post back again, I am trying this solution, but
    if( in_array( $title == $selection ) ) {

    Seems to lose my repeater. If I remove it all the repeater rows will show.

    The code I am using is below, any feedback on where I’ve gone wrong would be a huge help to me.

    <?php
         $selection = get_field('author_select');
              if( !empty( $selection ) ) {
                   if( have_rows( 'user_profile', 'option' ) ) {
    
                        while( have_rows( 'user_profile', 'option' ) ) {
                             the_row();
    
                             $title = get_sub_field('full_name');
    
                             if( in_array( $title == $selection ) ) {
    
                                  $title = get_sub_field('full_name');
                                  $org = get_sub_field('organization');
                                  $img = get_sub_field('profile_image'); 
                                  ?>
                                  <figure id="author">
                                       <div class="d-grid">
                                            <div class="left-contents">
                                                 <h4>ABOUT THE AUTHOR:</h4>
                                                 <div id="author-avatar"><img src="<?php echo $img; ?>" alt="<?php echo $title; ?>" /></div>
                                            </div>
                                            <div class="mid-contents">
                                                 <h5><?php echo $title; ?> | <?php echo $org; ?></h5>
                                            </div>
                                       </div>
                                  </figure>
                                  <?php
                                       }
                                  }
                             }
                        }
                   ?>
  • Sorry to bring up an old post but I’m having the exact same issue as nateads.

    Is anyone able to help with this one?

  • @pickles can you post your code?

    I actually don’t remember what I did to fix my issue but I will look back at my code tomorrow and compare it with what you have.

  • Yeah I had pretty much used the same code as above as.

    
    <?php
         $selection = get_field('sa_select_bookmaker', 'option');
              if( !empty( $selection ) ) {
                   if( have_rows( 'side_ads', 'option' ) ) {
    
                        while( have_rows( 'side_ads', 'option' ) ) {
                             the_row();
    
                             $title = get_sub_field('sa_bookmaker');
    
                             if( in_array( $title == $selection ) ) {
    
                                  $title = get_sub_field('sa_bookmaker');
                                  $org = get_sub_field('left_ad');
                                  $img = get_sub_field('right_ad'); 
                                  ?>
                                  <?php echo $title; ?> <?php echo $org; ?> <?php echo $img; ?> 
                                  <?php
                                       }
                                  }
                             }
                        }
                   ?>
  • This needs to be changed

    
    if( in_array( $title == $selection ) ) {
    

    to this if $selection is an array

    
    if( in_array( $title, $selection ) ) {
    

    or this if $selection is a single value

    
    if( $title == $selection ) {
    
  • Thank you! Appreciate the help 🙂

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

The topic ‘Select specific repeater rows from an options page’ is closed to new replies.