Support

Account

Home Forums Add-ons Repeater Field Create default rows for repeater

Solved

Create default rows for repeater

  • I have a repeater field that is assigned to all users called “quick_links”

    There is a subfield in repeater “quick_links” called “bookmarks” which is of type URL.

    This allows the user to add a list of frequently visited links.

    I can add and display all the rows from the repeater field on the user’s page.

    However, when a new user is created, the repeater field is empty.

    What if I want to add 3 rows with different url’s by default when a new user is created for its repeater field? The user can then edit or delete links as required.

    Please let me know the best way to achieve this..

  • I found a way to do this.. I want to know if this is the ideal way of achieving this?
    Is there another way to add the update code instead of else:

    
    <?php 
    $rowvalue = get_field('quick_links', 'user_' . $user_id);
    
    // check if the repeater field has rows of data
    if( have_rows('quick_links', 'user_' . $user_id) ):
    
        // loop through the rows of data
        while ( have_rows('quick_links', 'user_' . $user_id) ) : the_row();
    
            // Testing field output
            the_sub_field('icon', 'user_' . $user_id);
            echo "<br />";
            the_sub_field('title', 'user_' . $user_id);
            echo "<br />";
            the_sub_field('link', 'user_' . $user_id);
            echo "<br />";
            echo "<br />";
        endwhile;
    
    else :
    $updated_value = array
      (
      array("icon"=>"fa-align-justify","title"=>"Link 1","link"=>"http://www.hotmail.com"),
     array("icon"=>"fa-align-justify","title"=>"Link 2","link"=>"http://www.google.com"),
      array("icon"=>"fa-align-justify","title"=>"Link 3","link"=>"http://www.yahoo.com"),
      );
      
    update_field('quick_links', $updated_value , 'user_' . $user_id);
    
    endif;
    
    ?>
    
    
  • Hi @yavisht

    I think your code is good enough. You can also move it so the repeater is updated before you’re trying to show it like this:

    if( !have_rows('quick_links', 'user_' . $user_id) ){
        $updated_value = array(
            array("icon"=>"fa-align-justify","title"=>"Link 1","link"=>"http://www.hotmail.com"),
            array("icon"=>"fa-align-justify","title"=>"Link 2","link"=>"http://www.google.com"),
            array("icon"=>"fa-align-justify","title"=>"Link 3","link"=>"http://www.yahoo.com"),
        );
          
        update_field('quick_links', $updated_value , 'user_' . $user_id);
    }
    
    if( have_rows('quick_links', 'user_' . $user_id) ):
    
        // loop through the rows of data
        while ( have_rows('quick_links', 'user_' . $user_id) ) : the_row();
    
            // Testing field output
            the_sub_field('icon', 'user_' . $user_id);
            echo "<br />";
            the_sub_field('title', 'user_' . $user_id);
            echo "<br />";
            the_sub_field('link', 'user_' . $user_id);
            echo "<br />";
            echo "<br />";
        endwhile;
    
    endif;

    I hope this helps 🙂

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

The topic ‘Create default rows for repeater’ is closed to new replies.