Title: Complex Fields Separate Content
Last modified: September 1, 2016

---

# Complex Fields Separate Content

 *  Resolved [darktakua](https://wordpress.org/support/users/darktakua/)
 * (@darktakua)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/)
 * Hi,
 * I’ve recently switched from ACF to Carbon Fields. So far I’ve successfully built
   all the fields, however, I cannot figure out how to separate the content for 
   the complex fields. At the moment, if I have two complex fields, the information
   is next to each other.
 * In addition to separating the content, I would also like to have a default code
   that the field content will go into. For example, in this picture ([http://image.prntscr.com/image/1c537d0468de499aac40f39575201f82.png](http://image.prntscr.com/image/1c537d0468de499aac40f39575201f82.png)),
   the tabs and card would be the default code.
 * In this case, the code for the tabs is:
 *     ```
       <li class="tab col s6"><a href="#jpakootas">Pakootas (WA-5)</a></li>
               <li class="tab col s6"><a href="#pjayapal">Jayapal (WA-7)</a></li>
       ```
   
 * As I add more complex content, I’d like to be able to have the same HTML but 
   have different text in the href and a tags. For instance, if I added a third 
   complex field, it would have the same `li` tags but different content in the `
   <a>` tags and `href` attributes (denoted by `! CHANGED CONTENT !`:
 *     ```
       <li class="tab col s6"><a href="#! CHANGED CONTENT !">! CHANGED CONTENT ! (WA-! CHANGED CONTENT !)</a></li>
       ```
   
 * Hopefully I explained it enough. Let me know if I need to elaborate. Thanks for
   the help!
 * [https://wordpress.org/plugins/carbon-fields/](https://wordpress.org/plugins/carbon-fields/)

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

 *  Thread Starter [darktakua](https://wordpress.org/support/users/darktakua/)
 * (@darktakua)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538074)
 * Also, this is the complex field code for the above:
 *     ```
       Container::make( 'post_meta', __( 'Rep' ) )
       	->show_on_template( 'states.php' )
       	->add_fields( array(
       		Field::make( 'complex', 'reps', __( 'Reps' ) )
       			->setup_labels( array(
       				'plural_name' => __( 'Reps' ),
       				'singular_name' => __( 'Rep' ),
       			) )
       			->add_fields( array(
       				Field::make( 'text', 'rep_last_name', __( 'Last Name' ) )
       					->set_width( 25 )
       					->set_required( true ),
       				Field::make( 'text', 'rep_state_abb', __( 'State's Abbreviation ) )
       					->set_width( 33 ),
       				Field::make( 'text', 'rep_dist_num', __( 'Representative District Number' ) )
       					->set_width( 33 ),
       			) ),
       	) );
       ```
   
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538105)
 * Hi [@darktakua](https://wordpress.org/support/users/darktakua/)
 * I’m not sure I fully understand your problem. Are you trying to figure out how
   to approach the complex field front-end representation?
 * If that’s the case, here is some example code that might help you:
 *     ```
       <?php if ( $reps = carbon_get_the_post_meta( 'reps', 'complex' ) ): ?>
       	<ul>
       		<?php foreach ( $reps as $rep ): ?>
       			<li class="tab col s6"><a href="#<?php echo sanitize_title_with_dashes( $rep['rep_last_name'] ); ?>">
       				<?php echo $rep['rep_last_name'] . ' (' . $rep['rep_state_abb'] . '-' . $rep['rep_dist_num'] . ')'  ?>
       			</li>
       		<?php endforeach ?>
       	</ul>
       <?php endif ?>
       ```
   
 *  Thread Starter [darktakua](https://wordpress.org/support/users/darktakua/)
 * (@darktakua)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538109)
 * Hi, thanks for replying.
 * Yes, I am trying to display it on the front end. However, for each complex field,
   I would like to display it separate from each other. For instance, for the tabs
   at the top, as I add more complex fields, I’d like to have another `<li></li>`
   with the same default content with the field content changed.
 *  Thread Starter [darktakua](https://wordpress.org/support/users/darktakua/)
 * (@darktakua)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538110)
 * I tested it and this works perfectly! May I ask, is there a way to check for 
   multiple `carbon_get_the_post_meta`? For example, I want to pull from both complex
   fields of `reps` and `senators`. In addition, I was wondering what the `sanitize_title_with_dashes`
   did?
 * Also, thanks so much for the help! 😀 You guys have made a great plugin.
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538111)
 * I think I understand now, you are trying to add content to the tabs and the above
   code is only for the tabs navigation, is that correct?
 * To add the tab content you can try the following:
 * Add a new rich text field to the complex for the actual tab content
 * `Field::make( 'rich_text', 'rep_description', __( 'Description' ) )`
 * Update the front-end to use the new field (this is just an example, you have 
   to ajust it based on your project requirements)
 *     ```
       <?php if ( $reps = carbon_get_the_post_meta( 'reps', 'complex' ) ): ?>
       	<ul class="tabs-nav">
       		<?php foreach ( $reps as $rep ): ?>
       			<li class="tab col s6">
       				<a href="#<?php echo sanitize_title_with_dashes( $rep['rep_last_name'] ); ?>">
       					<?php echo $rep['rep_last_name'] . ' (' . $rep['rep_state_abb'] . '-' . $rep['rep_dist_num'] . ')'  ?>
       				</a>
       			</li>
       		<?php endforeach ?>
       	</ul>
       	<ul class="tabs-content">
       		<?php foreach ( $reps as $rep ): ?>
       			<li class="tab-content" id="<?php echo sanitize_title_with_dashes( $rep['rep_last_name'] ); ?>">
       				<?php echo wpautop( $rep['rep_description'] ); ?>
       			</li>
       		<?php endforeach ?>
       	</ul>
       <?php endif ?>
       ```
   
 * Hope this helps.
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538112)
 * > May I ask, is there a way to check for multiple carbon_get_the_post_meta?
 * [@darktakua](https://wordpress.org/support/users/darktakua/), about the field
   checks, you have to write the checks yourself, there is no other way.
 * > In addition, I was wondering what the sanitize_title_with_dashes did?
 * `sanitize_title_with_dashes` is just a WordPress function that is used to slugify
   text. You can read more about it in the WordPress Codex: [https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes](https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes)
 * > Also, thanks so much for the help! 😀 You guys have made a great plugin.
 * Glad to help!
 *  Thread Starter [darktakua](https://wordpress.org/support/users/darktakua/)
 * (@darktakua)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538113)
 * For the checks, for example, I wanted to do this `<?php if ( $reps = carbon_get_the_post_meta('
   reps', 'complex' ) ): ?>` but in addition to `reps`, I wanted to add `senators`.
   Is there any way to add to that line or do I have to do another check just adding`
   senators`?
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538114)
 * If you have two complex fields that are somehow related, you can declare 2 variables
   before the actual logic and check them afterwards, for example:
 *     ```
       $reps = carbon_get_the_post_meta( 'reps', 'complex' );
       $senators = carbon_get_the_post_meta( 'senators', 'complex' );
   
       if ( $reps && $senators ) {
           // additional logic and html goes here
       }
       ```
   
 *  Thread Starter [darktakua](https://wordpress.org/support/users/darktakua/)
 * (@darktakua)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538115)
 * Ahh okay got it. Thanks.
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538116)
 * You are welcome!
 * Closing the ticket, if you have more questions, consider opening a new one.
 * Happy coding 🙂

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

The topic ‘Complex Fields Separate Content’ 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/)

 * 10 replies
 * 2 participants
 * Last reply from: [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/complex-fields-separate-content/#post-7538116)
 * Status: resolved