Support

Account

Home Forums Add-ons Flexible Content Field How to get label for

Solved

How to get label for

  • Is there a way to get the label of the field ‘transport’ or ‘tips’, which are part of a flexible content loop in this set-up?

    <?php while(the_flexible_field("content")): ?>	
       <?php if(get_row_layout() == "intro_text"): ?>
        <div class="introtext">
         <?php the_sub_field("main_content"); ?>
        </div>		
       <?php elseif(get_row_layout() == "transport"): ?>
         <div class="transport">
    	<?php the_sub_field("transport_tekst"); ?>
         </div>
       <?php elseif(get_row_layout() == "tips"): ?>
         <div class="tips">
    	<?php the_sub_field("tips_tekst"); ?>
         </div>
       <?php else:
    	// No further input
        endif;?>	
    <?php endwhile; ?> 

    Var_dump(content) gave a key for ‘transport’ like ‘5854017337325’, and not something like ‘field_5854017337325’. Thanks!

  • Do you mean the label for the flex layout? I’m not sure that there is, at least I’ve never seen anything that suggest that it would be easy. Layouts are stored as part of the field definition. Do an export to code and you can see how it’s stored. Each layout is an array. You would need to use get_field_object() to get the flex field and then loop through the “layouts” sub array to find the one with that layout “name” and output the “label” value of that array element.

    Hopefully that makes sense after you look at the output of exporting.

  • Thank you for the reply and sorry for the delay in answering. Nevermind the properties (like key) of the layout object look different, the idea of looping through the subarray was indeed what did the trick. With help from a friend who’s a PHP veteran, the following loop gave the wanted values.

    To loop through all items at once:

    <?php
    foreach($content['layouts'] as $layout) {
        echo 'layout: ' . $layout['label'] . '<br>';
        foreach($layout['sub_fields'] as $subField) {
            echo 'field: ' . $subField['label'] . '<br>';
        }
    }
    ?>

    To put the item on the right spot, it’s necessary to point to the right index-number of the subarray.
    So for example (using the loop from the question), this worked for Highlights with index 1:

    <?php elseif(get_row_layout() == "highlights"): ?>	
    <?php echo $content['layouts'][1]['sub_fields'][0]['label']; //highlights ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to get label for’ is closed to new replies.