problem with complex fields groups
-
Hello,
i am doing my first project with carbon fields, and i use complex fields groups to make the same thing as acf flexible content, so i have differents layouts blocks that i want to reuse and display where i want
but the order of the groups stays the one written in the code, and not the order i give them in the pagefor if in the code i have function display_layouts(){
display layout_1();
display layout_2();
}on my page i will always have layout1 first and then layout 2
and if i have several layouts 1, they will be one after another, and then layout 2so if it works like that, i cannot use carbon fields in this way, as the complex field group is essential to my work
so tell me if it’s my mistake and i have done something wrong, or if carbon fields work like that, and in this case will it be corrected
-
btw, this is my code for the fields
use Carbon_Fields\Container; use Carbon_Fields\Field; Container::make( 'post_meta', 'layouts' ) ->show_on_post_type( 'page' ) ->show_on_page( 'home' ) ->add_fields( array( Field::make( 'complex', 'crb_block_layouts' ) ->add_fields( 'last_post',array( Field::make( 'text', 'featured_id' ) )) ->add_fields( 'four_next_posts',array( Field::make( 'text', 'four_next_posts_title' ) )) ->add_fields( 'four_block_posts', array( Field::make( 'text', 'four_block_post_title' ), Field::make( 'text', 'first_post_id' ), Field::make( 'text', 'three_side_posts_id' ), ) ) ->add_fields( 'two_col_posts', array( Field::make( 'text', 'two_col_posts_title' ), Field::make( 'complex', 'post_item' ) ->add_fields( array( Field::make( 'text', 'post_id' ) ) ) ) ) ->add_fields( 'text_area', array( Field::make( 'rich_text', 'text_editor' ), ) ) ) );Hey @alexadark,
You can name your section functions like this:
render_section_four_block_posts(),render_section_two_col_posts()etc.Then, when rendering the complex fields, you have the
_typearray value. You can use it to get the type of a complex section, then to call the correct section rendering function. So it would be something like this:$data = carbon_get_post_meta( get_the_ID(), 'crb_block_layouts', 'complex' ); foreach( $data as $entry ) { $function_name = 'render_section' . $entry['_type']; if ( function_exists( $function_name ) ) { call_user_func( $function_name, $entry ); } }Please, let us know if you need additional help.
Hi, my problem is not to render the fields
this is the site : http://cozy.webstantly.com/
all the sections of the home are a complex field groupthe problem is not to display them, is to change their order, they still in the order as it’s written in the code
and not the one of the back office, where i can drag and drop them to change this order, but it doesn;t change in frontbasically this is the code i use to display them (this is the code i used when i was trying the complex field before using it in a real project, so the code in the project is much more complex, but i realoze now that the demo as the same problem)
function wst_display_fields() { $layouts = carbon_get_the_post_meta( 'crb_layouts', 'complex' ); var_dump( $layouts ); if(isset($layouts)) { wst_display_driver($layouts); wst_display_teacher($layouts); } } function wst_display_driver($layouts){ foreach ($layouts as $layout){ if ( $layout['_type'] == '_driver' ) { $licence = $layout['license']; $name = $layout['name']; ?> <h2>Driver</h2> <?php echo $name; ?><br> <?php echo $licence ?><br> <?php } } } function wst_display_teacher($layouts){ foreach ($layouts as $layout){ if ( $layout['_type'] == '_teacher' ) { $picture = $layout['picture']; $year = $layout['years_of_experience']; ?> <h2>teacher</h2> <img src="<?php echo $picture ?>" alt=""> <?php echo $year ?> <?php } } }function wst_display_fields() { $layouts = carbon_get_the_post_meta( 'crb_layouts', 'complex' ); if(isset($layouts)) { wst_display_driver($layouts); wst_display_teacher($layouts); } } function wst_display_driver($layouts){ foreach ($layouts as $layout){ if ( $layout['_type'] == '_driver' ) { $licence = $layout['license']; $name = $layout['name']; ?> <h2>Driver</h2> <?php echo $name; ?><br> <?php echo $licence ?><br> <?php } } } function wst_display_teacher($layouts){ foreach ($layouts as $layout){ if ( $layout['_type'] == '_teacher' ) { $picture = $layout['picture']; $year = $layout['years_of_experience']; ?> <h2>teacher</h2> <img src="<?php echo $picture ?>" alt=""> <?php echo $year ?> <?php } } }Hello @alexadark,
Actually, it appears that the problem is exactly the displaying of field groups.
Feel free to change your wst_display_fields() function to what we’ve provided in the demo code above.
Please, let us know if you’re having further issues.
ok, i’m going to try that, and will tell you if it works
i’m sorry, but i don’t understand how to use this code
can you please write it concretely aplyed to these simple example fields
and tell me what is wrong in my code, from there i will be able to adaptContainer::make( 'post_meta', 'Custom Data' ) ->show_on_post_type( 'page' ) ->show_on_template( 'test-template.php' ) ->add_fields( array( Field::make( 'complex', 'crb_layouts' ) ->add_fields( 'driver', array( Field::make( 'text', 'name' ), Field::make( 'text', 'license' ), ) ) ->add_fields( 'teacher', array( Field::make( 'image', 'picture' ), Field::make( 'text', 'years_of_experience' ), ) ), ) );and then that was the function to display them
function wst_display_fields() { $layouts = carbon_get_the_post_meta( 'crb_layouts', 'complex' ); if(isset($layouts)) { wst_display_driver($layouts); wst_display_teacher($layouts); } } function wst_display_driver($layouts){ foreach ($layouts as $layout){ if ( $layout['_type'] == '_driver' ) { $licence = $layout['license']; $name = $layout['name']; ?> <h2>Driver</h2> <?php echo $name; ?><br> <?php echo $licence ?><br> <?php } } } function wst_display_teacher($layouts){ foreach ($layouts as $layout){ if ( $layout['_type'] == '_teacher' ) { $picture = $layout['picture']; $year = $layout['years_of_experience']; ?> <h2>teacher</h2> <img src="<?php echo $picture ?>" alt=""> <?php echo $year ?> <?php } } }Okay, please try replacing your display functions with these:
function wst_display_fields() { $layouts = carbon_get_the_post_meta( 'crb_layouts', 'complex' ); foreach ( $layouts as $layout ) { $function_name = 'wst_display' . $layout['_type']; if ( function_exists( '$function_name' ) ) { call_user_func( $function_name, $layout ); } } } function wst_display_driver($layout) { $licence = $layout['license']; $name = $layout['name']; ?> <h2>Driver</h2> <?php echo $name; ?><br> <?php echo $licence ?><br> <?php } function wst_display_teacher($layout) { $picture = $layout['picture']; $year = $layout['years_of_experience']; ?> <h2>teacher</h2> <img src="<?php echo $picture ?>" alt=""> <?php echo $year; }Note: best practices suggest that you should check any data for existence before displaying it. Also, the display functions currently lack some escaping and sanitization (https://codex.ww.wp.xz.cn/Validating_Sanitizing_and_Escaping_User_Data) – you’ll probably want to do that as well.
Please, let us know if you need further assistance.
Thank you it works now!, and i have understood, so i am going to adapt in my project
You really should put this type of simple examples on the doc site 🙂
Hello @alexadark,
We’re constantly working on improving the documentation. So yes, more examples and guides will be included in the future.
Thanks.
Another small thing but not so important
sometime i want to put a layout in a complex field group, but it has not necessary a custom field in it
imagine a separator for exemple, that i want to put there so the client can place it where he wants between sections
for the moment i am obliged to put a “fake custom field” to make it workThis makes sense, feel free to submit a new feature request in the Issues section in the GitHub repo: https://github.com/htmlburger/carbon-fields/issues
The topic ‘problem with complex fields groups’ is closed to new replies.