Adding a sidebar to a blog post only

Hi guys,

Been trying to add a sidebar to the blog page and all the articles by following this article https://theme.co/apex/forums/topic/adding-a-sidebar-to-a-blog-post-only but the provided code only adds sidebar for the articles, not the blog page.

Also tried to change the layout of the blog page from settings inside the builder but still nothing: https://prnt.sc/iu3di2

The layout of the website is set to fullwidth.

Your help would be really appreciated!

link: https://kryptonitecreative.com/trends

Hi There,

Please add the following code under functions.php file locates in your child theme:

function x_get_content_layout() {

		$content_layout = x_get_option( 'x_layout_content' );

		if ( $content_layout != 'full-width' ) {
			if ( is_home() ) {
				$opt    = x_get_option( 'x_blog_layout' );
				$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
			} elseif ( is_singular( 'post' ) ) {
				$meta   = get_post_meta( get_the_ID(), '_x_post_layout', true );
				$layout = ( $meta == 'on' ) ? 'full-width' : $content_layout;
			} elseif ( x_is_portfolio_item() ) {
				$layout = 'full-width';
			} elseif ( x_is_portfolio() ) {
				$meta   = get_post_meta( get_the_ID(), '_x_portfolio_layout', true );
				$layout = ( $meta == 'sidebar' ) ? $content_layout : $meta;
			} elseif ( is_page_template( 'template-layout-content-sidebar.php' ) ) {
				$layout = 'content-sidebar';
			} elseif ( is_page_template( 'template-layout-sidebar-content.php' ) ) {
				$layout = 'sidebar-content';
			} elseif ( is_page_template( 'template-layout-full-width.php' ) ) {
				$layout = 'full-width';
			} elseif ( is_archive() ) {
				if ( x_is_shop() || x_is_product_category() || x_is_product_tag() ) {
					$opt    = x_get_option( 'x_woocommerce_shop_layout_content' );
					$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
				} else {
					$opt    = x_get_option( 'x_archive_layout' );
					$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
				}
			} elseif ( x_is_product() ) {
				$layout = 'content-sidebar';
			} elseif ( x_is_bbpress() ) {
				$opt    = x_get_option( 'x_bbpress_layout_content' );
				$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
			} elseif ( x_is_buddypress() ) {
				$opt    = x_get_option( 'x_buddypress_layout_content' );
				$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
			} elseif ( is_404() ) {
				$layout = 'full-width';
			} else {
				$layout = $content_layout;
			}
		} else {
			$layout = $content_layout;
		}

		if ( is_singular( 'post' ) ) {
			$layout = 'content-sidebar';
		}

		return $layout;

}

Hope it helps :slight_smile:

1 Like

Hi,

I am unable to view the sidebar on my blog post page.

https://www.staging5.hotwax.co/new-ebook-helps-cios-coos-identify-critical-operational-breakdowns/

Thank you

Hi,

I used this function : function x_get_content_layout() {

	$content_layout = x_get_option( 'x_layout_content' );

	if ( $content_layout != 'full-width' ) {
		if ( is_home() ) {
			$opt    = x_get_option( 'x_blog_layout' );
			$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
		} elseif ( is_singular( 'post' ) ) {
			$meta   = get_post_meta( get_the_ID(), '_x_post_layout', true );
			$layout = ( $meta == 'on' ) ? 'full-width' : $content_layout;
		} elseif ( x_is_portfolio_item() ) {
			$layout = 'full-width';
		} elseif ( x_is_portfolio() ) {
			$meta   = get_post_meta( get_the_ID(), '_x_portfolio_layout', true );
			$layout = ( $meta == 'sidebar' ) ? $content_layout : $meta;
		} elseif ( is_page_template( 'template-layout-content-sidebar.php' ) ) {
			$layout = 'content-sidebar';
		} elseif ( is_page_template( 'template-layout-sidebar-content.php' ) ) {
			$layout = 'sidebar-content';
		} elseif ( is_page_template( 'template-layout-full-width.php' ) ) {
			$layout = 'full-width';
		} elseif ( is_archive() ) {
			if ( x_is_shop() || x_is_product_category() || x_is_product_tag() ) {
				$opt    = x_get_option( 'x_woocommerce_shop_layout_content' );
				$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
			} else {
				$opt    = x_get_option( 'x_archive_layout' );
				$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
			}
		} elseif ( x_is_product() ) {
			$layout = 'content-sidebar';
		} elseif ( x_is_bbpress() ) {
			$opt    = x_get_option( 'x_bbpress_layout_content' );
			$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
		} elseif ( x_is_buddypress() ) {
			$opt    = x_get_option( 'x_buddypress_layout_content' );
			$layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
		} elseif ( is_404() ) {
			$layout = 'full-width';
		} else {
			$layout = $content_layout;
		}
	} else {
		$layout = $content_layout;
	}

	if ( is_singular( 'post' ) ) {
		$layout = 'content-sidebar';
	}

	return $layout;

}

But now the widgets are not displaying properly.

Hi there,

That code will only enable the sidebar placeholder, you’ll still need to add your widgets. What widgets do you expect to see? You can also assign a custom sidebar to your post and add your preferred widgets to that sidebar.

I checked your last URL and there is no sidebar, you probably removed the code.

Thanks!

Hello.

I’m trying to do what OP is trying, too. (Get the sidebar on blog posts AND the blog index)

I added in the code to my child theme’s fucntions.php, but like OP said above, it only adds the sidebar to the individual blog post pages. How can I make it so it shows up on the blog index page (list of my blog posts) too?

Thanks!

Hello @aleminopuy,

Kindly double check if you set your posts page in the Reading Settings pane.

That should be your blog page.

Also try to check the layout you’ve set in Theme Options > Blog > Layout. Make sure it’s set to global.

Please create a separate thread next time to avoid confusion upon replying.

Hope that helps. Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.