Support

Account

Home Forums Add-ons Flexible Content Field Output multiple select fields in unordered list?

Solved

Output multiple select fields in unordered list?

  • Hi – I’ve successfully outputted the contents of a multiple select field on my web page using following code in page template:

    if( have_rows('flexible_content_field_name') ):
       while ( have_rows('flexible_content_field_name') ) : the_row();
    
    		if( get_row_layout() == 'likes' ):
    		
    			$likes_list = get_sub_field('likes_list');
    			echo implode(', ', $likes_list);
            endif;
    
       endwhile;
    
    endif;

    Page output is : item1, item2, item3

    I’d like to break up the implode(', ', $likes_list); code so that I can make my the items output into an unordered list:

    • item1
    • item2
    • item3

    Any help much appreciated!

  • Hi @nadgemanforum

    It’s just a matter of looping through the values (it’s an array) and output an li for each!

    This should do it:

    
    if( have_rows('flexible_content_field_name') ):
       while ( have_rows('flexible_content_field_name') ) : the_row();
    
    		if( get_row_layout() == 'likes' ):
    			$likes_list = get_sub_field('likes_list');
    			if( $likes_list ):
    				echo '<ul>';
    				foreach( $likes_list as $like ):
    					echo '<li>' . $like . '</li>';
    				endforeach;
    				echo '</ul>';
    			endif;
    			echo implode(', ', $likes_list);
            endif;
    
       endwhile;
    
    endif;
    

    Best of luck in your project!

  • Thanks for your help Jonathan.

    Just one thing: “echo implode(‘, ‘, $likes_list);” no longer required!

    Thanks again.

  • @nadgemanforum

    Lol.. I copied your code and forgot to delete it 😉

    Anyway, you’re welcome! If you have more questions regarding ACF feel free to write in the forums.

  • How can I create a bullet list without having to code? I’m new to ACF and using CPT UI to set things up.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.