Title: problem with complex fields groups
Last modified: August 31, 2016

---

# problem with complex fields groups

 *  Resolved [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/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 page
 * for 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 2
 * so 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
 * [https://wordpress.org/plugins/carbon-fields/](https://wordpress.org/plugins/carbon-fields/)

Viewing 11 replies - 1 through 11 (of 11 total)

 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352339)
 * 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' ),
       		         ) )
                ) );
       ```
   
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352502)
 * Hey [@alexadark](https://wordpress.org/support/users/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 `_type` array 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.
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352503)
 * Hi, my problem is not to render the fields
    this is the site : [http://cozy.webstantly.com/](http://cozy.webstantly.com/)
   all the sections of the home are a complex field group
 * the 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 front
 * basically 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 }
   
       	}
       }
       ```
   
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352504)
 * Hello [@alexadark](https://wordpress.org/support/users/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.
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352505)
 * ok, i’m going to try that, and will tell you if it works
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352509)
 * 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 adapt
 *     ```
       Container::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 }
   
       	}
       }
       ```
   
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352510)
 * 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.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data](https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data))–
   you’ll probably want to do that as well.
 * Please, let us know if you need further assistance.
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352511)
 * 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 🙂
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352512)
 * Hello [@alexadark](https://wordpress.org/support/users/alexadark/),
 * We’re constantly working on improving the documentation. So yes, more examples
   and guides will be included in the future.
 * Thanks.
 *  Thread Starter [alexadark](https://wordpress.org/support/users/alexadark/)
 * (@alexadark)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352513)
 * 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 work
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352514)
 * This 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](https://github.com/htmlburger/carbon-fields/issues)

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘problem with complex fields groups’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/carbon-fields_cacbcc.svg)
 * [Carbon Fields](https://wordpress.org/plugins/carbon-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/carbon-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/carbon-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/carbon-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/carbon-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/carbon-fields/reviews/)

## Tags

 * [flexible content](https://wordpress.org/support/topic-tag/flexible-content/)

 * 11 replies
 * 2 participants
 * Last reply from: [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/problem-with-complex-fields-groups/#post-7352514)
 * Status: resolved