Support

Account

Home Forums Add-ons Flexible Content Field Flexible Content "key"

Solved

Flexible Content "key"

  • When using a flexible content layout, I assume each layout added to the page has some sort of unique “key” value for it.

    Is there a way to retrieve this value?

    I am wanting to use it to create unique id’s for each item in the layout for styling purposes.

    eg: $key = THE THING I AM MISSING
    then…

    echo '<div id="layout_'.$key.'">';

    etc…..

  • Hi @frysteen

    I believe you are looking for the get_row_layout() function as seen in the flexible content documentation: http://www.advancedcustomfields.com/resources/field-types/flexible-content/

    Thanks
    E

  • Hi Elliot, I have all my layouts working fine and use the get_row_layout() to retrieve the corresponding layout file with no dramas.

    What I am after is a unique identifier for each layout module.

    For example, if I have two Gallery modules on the same flexible content area, I assume each one has a different ID of some kind so that the images from Gallery A are not mixed with images from Gallery B.

    So when looping over the modules, is there an ID associated with each module added in the back end?

    something like this….

    if( get_row_layout() == 'paragraph' ):
     
        key_id = ??????
        the_sub_field('text');
     
    elseif( get_row_layout() == 'download' ): 
     
        key_id = ??????
        $file = get_sub_field('file');
     
    endif;

    Hence, each Paragraph added to the page with have unique KEY for it.

    I hope I am making sense and not missing something that is easy 🙂

    Thanks, Sam

  • Hi @frysteen

    The data is saved to the database using a numeric counter as the $key.

    So you could achieve this on the front end by introducing a counter like so:

    
    <?php 
    
    $i = -1;
    
    while( have_rows() ) {
    	
    	the_row();
    	
    	$i++;
    	
    	if( get_row_layout() == 'paragraph' ):
    	 	
    	 	echo "key={$i}";
    	    the_sub_field('text');
    	 
    	elseif( get_row_layout() == 'download' ): 
    	 
    	    echo "key={$i}";
    	    $file = get_sub_field('file');
    	 
    	endif;
    	
    }
     ?>
    

    I hope that helps.

    Thanks
    E

  • Perfect…. That will work a treat. Not sure why I didn’t think of that.

    Thanks @elliot, you da man!

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

The topic ‘Flexible Content "key"’ is closed to new replies.