Title: Custom Functions To Access Complex Data Dynamically
Last modified: September 1, 2016

---

# Custom Functions To Access Complex Data Dynamically

 *  Resolved [Jon Waldstein](https://wordpress.org/support/users/jonwaldstein/)
 * (@jonwaldstein)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/custom-functions-to-access-complex-data-dynamically/)
 * Hello,
 * I am want to use carbon fields to create a custom function that checks to see
   if “enable full width” has been selected inside a complex field, if so add a 
   class, if not add a class.
 * below is my function, complex field, and html:
 *     ```
       if (!function_exists('enable_full_width_section')) {
         	function enable_full_width_section(){
       		if (carbon_get_the_post_meta(get_the_ID(), 'enable_full_width_section')){
       	      	$class = 'container-fluid';
       	    	} else{
       	      	$class = 'container';
       	    	}
       	    echo $class;
       	}
        }
       ```
   
 *     ```
       Container::make( 'post_meta', 'Custom Data' )
                ->show_on_post_type( 'page' )
                ->add_fields( array(
   
       	         Field::make( 'complex', 'crb_layouts' )
       	              ->add_fields( 'two_column_layout', array(
       	              	  Field::make('checkbox', 'enable_full_width_section')->set_option_value('yes'),
       		              Field::make( 'rich_text', 'column_left' )->set_width(50),
       		              Field::make( 'rich_text', 'column_right' )->set_width(50),
       	              ) )
       	              ->add_fields( 'full_width_layout', array(
       	              	Field::make('checkbox', 'full_width_section')->set_option_value('yes'),
       		            Field::make( 'rich_text', 'column' ),
       	              )),
                ) );
       ```
   
 *     ```
       function zgm_two_column_layout($layout) {
       	$column_left = $layout['column_left'];
       	$column_right = $layout['column_right'];
       	?>
       	<div class="section <?php enable_full_width_section(); ?>">
       		<div class="row">
       			<div class="col-sm-6">
       				<?php echo $column_left; ?>
       			</div>
       			<div class="col-sm-6">
       				<?php echo $column_right; ?>
       			</div>
       		</div>
       	</div>
       	<?php
       }
       ```
   
 * [https://wordpress.org/plugins/carbon-fields/](https://wordpress.org/plugins/carbon-fields/)

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

 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/custom-functions-to-access-complex-data-dynamically/#post-7713747)
 * Hi [@jonwaldstein](https://wordpress.org/support/users/jonwaldstein/),
 * You have to handle each complex group separately. If you have a two columns layout
   and a full-width layout and both can have a full-width option you can do the 
   following:
 *     ```
       Container::make( 'post_meta', 'Custom Data' )
       	->show_on_post_type( 'page' )
       	->add_fields( array(
       		Field::make( 'complex', 'crb_layouts' )
       			->add_fields( 'two_column_layout', array(
       				Field::make( 'checkbox', 'full_width_section' ),
       				Field::make( 'rich_text', 'column_left' )->set_width(50),
       				Field::make( 'rich_text', 'column_right' )->set_width(50),
       			) )
       			->add_fields( 'full_width_layout', array(
       				Field::make( 'checkbox', 'full_width_section' ),
       				Field::make( 'rich_text', 'column' ),
       			)),
        ) );
       ```
   
 *     ```
       function zgm_print_layout($layout) {
       	?>
       	<div class="section <?php echo $layout['full_width_section'] ? 'container-fluid' : 'container'; ?>">
       		<div class="row">
       			<?php if ( $layout['_type'] === '_two_column_layout' ): ?>
       				<div class="col-sm-6">
       					<?php echo $layout['column_left']; ?>
       				</div>
       				<div class="col-sm-6">
       					<?php echo $layout['column_right']; ?>
       				</div>
       			<?php elseif ( $layout['_type'] === '_full_width_layout' ): ?>
       				<div class="col">
       					<?php echo $layout['column']; ?>
       				</div>
       			<?php endif ?>
       		</div>
       	</div>
       	<?php
       }
       ```
   
 * Hope this helps.
 *  Thread Starter [Jon Waldstein](https://wordpress.org/support/users/jonwaldstein/)
 * (@jonwaldstein)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/custom-functions-to-access-complex-data-dynamically/#post-8152707)
 * Thank you for the fast reply. Your example was a big help in understanding more
   about carbon fields. For anyone curious, I have implemented a background image
   function below inside my print_layout function.
 *     ```
       if (!function_exists('bg_image')){
       		function bg_image($bg){
       			if ($bg){
       				$background_image = "background:url('".$bg."');background-size:cover;background-repeat:no-repeat;background-position: center center;";
       	        	echo $background_image;
       			}
       		}
       	}
       ```
   
 * Then you can do
    `<div class="section style="<?php bg_image($layout['section_background_image']);?
   >">`

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

The topic ‘Custom Functions To Access Complex Data Dynamically’ 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

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

 * 2 replies
 * 2 participants
 * Last reply from: [Jon Waldstein](https://wordpress.org/support/users/jonwaldstein/)
 * Last activity: [9 years, 8 months ago](https://wordpress.org/support/topic/custom-functions-to-access-complex-data-dynamically/#post-8152707)
 * Status: resolved