Support

Account

Home Forums Add-ons Repeater Field Repeater field doesn’t work with only one entry

Unread

Repeater field doesn’t work with only one entry

  • I’ve created an options page and on that page I attached a repeater field with one link field in it.

    The repeater field on the options page is called: featured_links.
    The subfield is called ftlink and it returns an array.

    My code to view the fields is this:

    function uplvl_favlist () {
        if ( get_field( 'featured_links', 'option' ) ) {
            $links = '<div class="favLinks">';
    
            //check if there are external links
            if ( have_rows( 'featured_links', 'option' ) ):
                while ( have_rows( 'featured_links', 'option' ) ) : the_row();
                    $link = get_sub_field('ftlink');
                    $url = $link['url'];
                    $title = $link['title'];
                    $target = $link['target'] ? $link['target'] : '_self';
                    $links .='<a href="'.esc_url($url).'" target="'. esc_attr( $target ) .'" rel="noopener">'.esc_html($title).'</a>';
                endwhile;
            endif;
            $links .= '</div>';
            
            return $links;
        } else {
            return;
        }
    }
    add_shortcode('fav-list', 'uplvl_favList');

    If on the options page I enter just one link (url:https://example.com title: example), I get an error:
    Uncaught Error: Cannot access offset of type string on string
    referencing $link = get_sub_field('ftlink');

    however if I add 2nd row entry to the field the code returns as expected 2 links.

    What am I doing incorrectly?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.