Skip to content

When you are creating a post or page in Divi, more often than not, you are ‘forced’ to do away with the good old sidebar. But I have found myself wanting to combine the best of both worlds. I want to use the Divi editor with different sidebars on different posts or pages.

Sometimes, you don’t want to create columns, especially for your posts. Why can’t the post editor do just one thing  –  create posts? I have the answer to your cravings. Take a look at the image on the left.

Guess what? It works with pages too. In fact, whether you use page builder or not, as long as you’re using Divi or Divi child theme you can surely enjoy this.

So, how do you create this? Follow me.

1. Copy the code to functions.php

Every theme has a functions.php. Copy the three code blocks below and paste it in your functions.php.

function et_single_settings_meta_box( $post ) {
	$post_id = get_the_ID();

	wp_nonce_field( basename( __FILE__ ), 'et_settings_nonce' );

	$page_layout = get_post_meta( $post_id, '_et_pb_page_layout', true );

	$side_nav = get_post_meta( $post_id, '_et_pb_side_nav', true );
	
	$selected_sidebar = get_post_meta( $post_id, '_et_pb_sidebar_figarts', true );

	$project_nav = get_post_meta( $post_id, '_et_pb_project_nav', true );

	$post_hide_nav = get_post_meta( $post_id, '_et_pb_post_hide_nav', true );
	$post_hide_nav = $post_hide_nav && 'off' === $post_hide_nav ? 'default' : $post_hide_nav;

	$show_title = get_post_meta( $post_id, '_et_pb_show_title', true );

	$page_layouts = array(
		'et_right_sidebar'   => esc_html__( 'Right Sidebar', 'Divi' ),
		'et_left_sidebar'    => esc_html__( 'Left Sidebar', 'Divi' ),
		'et_full_width_page' => esc_html__( 'Full Width', 'Divi' ),
	);

	$sidebars = allsidebars();

	$layouts = array(
		'light' => esc_html__( 'Light', 'Divi' ),
		'dark'  => esc_html__( 'Dark', 'Divi' ),
	);
	$post_bg_color  = ( $bg_color = get_post_meta( $post_id, '_et_post_bg_color', true ) ) && '' !== $bg_color
		? $bg_color
		: '#ffffff';
	$post_use_bg_color = get_post_meta( $post_id, '_et_post_use_bg_color', true )
		? true
		: false;
	$post_bg_layout = ( $layout = get_post_meta( $post_id, '_et_post_bg_layout', true ) ) && '' !== $layout
		? $layout
		: 'light'; ?>

	<p class="et_pb_page_settings et_pb_page_layout_settings">
		<label for="et_pb_page_layout" style="display: block; font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Page Layout', 'Divi' ); ?>: </label>

		<select id="et_pb_page_layout" name="et_pb_page_layout">
		<?php
		foreach ( $page_layouts as $layout_value => $layout_name ) {
			printf( '<option value="%2$s"%3$s>%1$s</option>',
				esc_html( $layout_name ),
				esc_attr( $layout_value ),
				selected( $layout_value, $page_layout, false )
			);
		} ?>
		</select>
	</p>
	<p class="et_pb_page_settings et_pb_side_nav_settings" style="display: none;">
		<label for="et_pb_side_nav" style="display: block; font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Dot Navigation', 'Divi' ); ?>: </label>

		<select id="et_pb_side_nav" name="et_pb_side_nav">
			<option value="off" <?php selected( 'off', $side_nav ); ?>><?php esc_html_e( 'Off', 'Divi' ); ?></option>
			<option value="on" <?php selected( 'on', $side_nav ); ?>><?php esc_html_e( 'On', 'Divi' ); ?></option>
		</select>
	</p>
	<p class="et_pb_page_settings">
		<label for="et_pb_post_hide_nav" style="display: block; font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Hide Nav Before Scroll', 'Divi' ); ?>: </label>

		<select id="et_pb_post_hide_nav" name="et_pb_post_hide_nav">
			<option value="default" <?php selected( 'default', $post_hide_nav ); ?>><?php esc_html_e( 'Default', 'Divi' ); ?></option>
			<option value="no" <?php selected( 'no', $post_hide_nav ); ?>><?php esc_html_e( 'Off', 'Divi' ); ?></option>
			<option value="on" <?php selected( 'on', $post_hide_nav ); ?>><?php esc_html_e( 'On', 'Divi' ); ?></option>
		</select>
	</p>

	<p class="et_pb_page_settings">
		<label for="_et_pb_sidebar_figarts" style="display: block; font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Sidebar', 'Divi' ); ?>: </label>
		<select id="_et_pb_sidebar_figarts" name="_et_pb_sidebar_figarts">
			<?php $wp_registered_sidebars = allsidebars(); ?>
			<?php foreach ( $wp_registered_sidebars as $sidebar ) : ?>
				<option value="<?php echo $sidebar['id']; ?>" <?php selected( $sidebar['id'], $selected_sidebar ); ?>><?php esc_html_e( $sidebar['name'], 'Divi' ); ?></option>
			<?php endforeach; ?>
		</select>
	</p>

<?php if ( 'post' === $post->post_type ) : ?>
	<p class="et_pb_page_settings et_pb_single_title" style="display: none;">
		<label for="et_single_title" style="display: block; font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Post Title', 'Divi' ); ?>: </label>

		<select id="et_single_title" name="et_single_title">
			<option value="on" <?php selected( 'on', $show_title ); ?>><?php esc_html_e( 'Show', 'Divi' ); ?></option>
			<option value="off" <?php selected( 'off', $show_title ); ?>><?php esc_html_e( 'Hide', 'Divi' ); ?></option>
		</select>
	</p>

	<p class="et_divi_quote_settings et_divi_audio_settings et_divi_link_settings et_divi_format_setting et_pb_page_settings">
		<label for="et_post_use_bg_color" style="display: block; font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Use Background Color', 'Divi' ); ?></label>
		<input name="et_post_use_bg_color" type="checkbox" id="et_post_use_bg_color" <?php checked( $post_use_bg_color ); ?> />
	</p>

	<p class="et_post_bg_color_setting et_divi_format_setting et_pb_page_settings">
		<input id="et_post_bg_color" name="et_post_bg_color" class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value', 'Divi' ); ?>" value="<?php echo esc_attr( $post_bg_color ); ?>" data-default-color="#ffffff" />
	</p>

	<p class="et_divi_quote_settings et_divi_audio_settings et_divi_link_settings et_divi_format_setting">
		<label for="et_post_bg_layout" style="font-weight: bold; margin-bottom: 5px;"><?php esc_html_e( 'Text Color', 'Divi' ); ?>: </label>
		<select id="et_post_bg_layout" name="et_post_bg_layout">
	<?php
		foreach ( $layouts as $layout_name => $layout_title )
			printf( '<option value="%s"%s>%s</option>',
				esc_attr( $layout_name ),
				selected( $layout_name, $post_bg_layout, false ),
				esc_html( $layout_title )
			);
	?>
		</select>
	</p>
<?php endif;
}

This adds the Sidebar field to your “Divi Post Settings” or “Divi Page Settings”.

function allsidebars(){
  global $wp_registered_sidebars;
  if ( !empty( $wp_registered_sidebars ) )
      return $wp_registered_sidebars;
}
add_action('init', 'allsidebars');

This is used to get all sidebars and those created with Divi in the widget area.

function figarts_divi_post_settings_save_details( $post_id, $post ){
	global $pagenow;

	if ( 'post.php' != $pagenow ) return $post_id;

	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
		return $post_id;

	$post_type = get_post_type_object( $post->post_type );
	if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) )
		return $post_id;

	if ( ! isset( $_POST['et_settings_nonce'] ) || ! wp_verify_nonce( $_POST['et_settings_nonce'], basename( __FILE__ ) ) )
		return $post_id;

	if ( isset( $_POST['_et_pb_sidebar_figarts'] ) )
		update_post_meta( $post_id, '_et_pb_sidebar_figarts', sanitize_text_field( $_POST['_et_pb_sidebar_figarts'] ) );
	 else 
			delete_post_meta( $post_id, '_et_pb_sidebar_figarts' );

}
add_action( 'save_post', 'figarts_divi_post_settings_save_details', 10, 2 );

Then, this saves your selected sidebar.

2. Create different widgets

Divi enables you to create several sidebars or widget areas. Go to WordPress Dashboard and click Appearance->Widgets and create as many widget areas as you like.

3. Adjust your Sidebar Code

Now, to wrap it up, adjust your sidebar.php file in your Divi Child Theme. If your child theme does not have a sidebar.php, create one. If you have, replace the code with this:

<?php
if ( ( is_single() || is_page() ) && 'et_full_width_page' === get_post_meta( get_queried_object_id(), '_et_pb_page_layout', true ) )
	return;

$sidebar = get_post_meta( get_the_ID(), '_et_pb_sidebar_figarts', true );

if ( is_active_sidebar( $sidebar ) ) : ?>
	<div id="sidebar">
		<?php dynamic_sidebar( $sidebar ); ?>
	</div> <!-- end #sidebar -->
<?php endif; ?>

4 responses to “DIVI: Choose Different Sidebars in Posts and Pages

  1. Hi,
    I followed all the steps outlined above.
    When adding a new page, I see the following under “Divi Page Settings” on the right.
    Page Layout: Right Sidebar / Left Sidebar / Full Width Options
    as well as the Hide Nav Before Scroll & Sidebar List Options.
    However, when I click on the “Use Divi Builder” button to create all the sections and rows for the page, the Page Layout : Option disappears and I see the Dot Navigation: Option with the rest.
    Why is this so?
    Could someone please explain where I am going wrong? Thank you very much for your time and patience.

  2. hi,

    i have the same problem as Susila. It work perfect and sidebar switch box is visible until i click “use layout builder”. than its gone.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.