Home › Forums › Add-ons › Flexible Content Field › Get subfield label dynamically
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>
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 🙂
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.
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.
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!
The topic ‘Get subfield label dynamically’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.