Home Forums Add-ons Flexible Content Field Get subfield label dynamically


Get subfield label dynamically

  • Hey,

    I use the flexible content fields for allot of fields, like this;

    <?php elseif(get_row_layout() == "the_field_name"): ?>
    <li><label>The Field Name</label>
    <?php get_template_part('incl/2r', 'tekst'); ?></li>

    Is it possible to get the field label dynamically so I don’t have to type out each field name as label, but instead get something like this.

    <?php elseif(get_row_layout() == "the_field_name"): ?>
    <li><label><?php the_field_label(); ?></label>
    <?php get_template_part('incl/2r', 'tekst'); ?></li>
  • Hi @Asitis

    Perhaps you will find the get_sub_field_object function to your liking.
    This will return an array containing all the field data such as ‘label’.


  • I also would like print out the layout label, but “get_sub_field_object” hasn’t helped me, maybe I’m using it wrong.
    Made it in two ways: ACF style “has_sub_field” and also more PHP style `$fields = get_field(‘flexible’);
    foreach($fields as $field)…`

    Printing out flexible content field (“get_field” and “get_field_object”) gives me a large array and I can see it’s contents, but I don’t get, how [value] is related to [layouts]. Therefore I cannot get the layout label.

    Could you advise me on how to get it?
    Since I’m using flexible->repeater->repeater with complicated HTML, I would much more prefer “foreach” style.

    Thank you 🙂

  • Hi

    Please create a new topic on monday morning, as your issue is not related to this solved topic. Also, can you please describe the issue with better clarity and include some code snippets to demonstrate what you are doing and what isn’t working.


  • @elliot,
    I’m using the same forum topic because the subject is strictly related..

    what we’re needing is a function like: get_layout_object()

    using the get_sub_field_object() will give us the object of certain field inside the layout, but we don’t need this, we need the layout label.

    Any clues?

  • Hi Guys!

    I was able to solve this. If you’re using PHP 5.5+ then you can do it like this:

    // get flexible content field object
    $layout = get_field_object( 'flexible_content_field' );
    // find current layout
    $key    = array_search( get_row_layout(), array_column( $layout['layouts'], 'name' ) );
    // output label
    echo $layout['layouts'][ $key ]['label'];

    Hope this helps!

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

The topic ‘Get subfield label dynamically’ is closed to new replies.