Different sidebar for different post types

Hi folks!

So, I have my blog and blog index using my Main Sidebar. I also want a sidebar to display on my individual Custom Post Types, called support_articles. I’ve used the following to pull the general sidebar through, but I really want to display a different sidebar on these sypport_articles, is there anyway to do this? (N.B. The X Theme custom/unlimited sidebar functionality isn’t working, which is why I’ve gone straight to a function.

This is the code I’m using to force my ‘standard’ sidebar onto Support Articles. But how can I do this with an alternative sidebar?

// 10. Add Sidebar to Support Articles
// =============================================================================
function sidebars_in_support_articles($contents) {
  if ( is_singular( 'support_article' ) ) {
    $contents = 'content-sidebar';
  }
  return $contents;
}
add_filter('x_option_x_layout_content', 'sidebars_in_support_articles', 5);

Hi,

You can add this code as well.

/* assign sidebar to support articles */
add_filter( 'ups_sidebar', 'support_articles_sidebar' );
function support_articles_sidebar($sidebar){
	if(is_singular( 'support_article' )){
		return 'ups-sidebar-support_article';
	}
	return $sidebar;
}

Make sure you have created a sidebar Support Article under Appearance > Sidebars

Hope that helps.

Awesome, thanks @paul.r. Fantastic support as always. I do have one more question…

This is all working perfectly, I have my individual CPT posts showing the correct Sidebar using this:

// 10. Give Support Articles The Support Sidebar
// =============================================================================
// 10a. Assign Default Sidebar to Support Articles
add_filter('x_option_x_layout_content', 'sidebars_in_support_articles', 5);
function sidebars_in_support_articles($contents) {
  if ( is_singular( 'support_article' ) ) {
    $contents = 'content-sidebar';
  }
  return $contents;
}

// 10b. Override Default Sidebar with Alternative for Support Articles
add_filter( 'ups_sidebar', 'support_articles_sidebar' );
function support_articles_sidebar($sidebar){
	if( is_singular( 'support_article' )){
		return 'ups-sidebar-support-sidebar';
	}
	return $sidebar;
}

I’m also listing these CPT on a standard ‘page’ and I’d like to add the sidebar to just this page too.

So far I have done the below and the page is showing the area for the sidebar (the layout is adjusting to display the sidebar on the right), but the actual sidebar is not showing. Why is this? Any advice?

// 11. Give Support Centre Index The Support Sidebar Sidebar
// =============================================================================
// 11a. Assign Default Sidebar to Support Centre Index
add_filter('x_option_x_layout_content', 'sidebars_in_support_index', 5);
function sidebars_in_support_index($contents) {
  if ( is_page ( 'support-centre' ) ) {
    $contents = 'content-sidebar';
  }
  return $contents;
}

// 11b. Override Default Sidebar with Alternative for Support Centre Index
add_filter( 'ups_sidebar', 'support_index_sidebar', 5 ); //testing 5
function support_index_sidebar($sidebar){
  if ( is_page ( 'support-centre' ) ) {
		return 'ups-sidebar-support-sidebar';
	}
	return $sidebar;
}

Any help would be greatly appreciated as I’m at the final hurdle I reckon!!

Hello There,

Thanks for updating in! Your code may not work because it is referencing to a non existent page slug. Please make sure that the page slug is support-centre or maybe you can just use the page ID is_page(123). To know where to find the page ID, you can check this out: https://theme.co/apex/forum/t/setup-how-to-locate-post-ids/59

Or maybe, you can make use of the page title like is_page('Support Center').

Hope this helps.

Hi @RueNel, thanks again for the help!

I think the slug was correct, but I changed to using the ID instead and I still have the same situation; the page layout has adjusted as if there should be a sidebar there, but the sidebar is not showing (even though it is showing correctly on the individual posts).

I’ll include a link to the page in a private reply, in case that helps.

Any ideas?

Hi There,

Make sure that you have created a sidebar Support Sidebar under Appearance > Sidebars. If yes, please do share admin credentials on a secure note so we can double check your setup.

Please ignore my previous post.

Hi There,

Unfortunately, I can’t access the site.

Apologies, that’s on my dev server. I crashed it last night and forgot to restart it! Should be back up now.

Hello Mike,

We still cannot login because the password seems to be invalid. Please double check it.

By the way, is your site layout is fullwidth? Please edit your Support Center page and change the page template to Layout - Sidebar Left, Content Right or Layout - Content Left, Sidebar Right page template. This could be the reason why there is no sidebar on the page.

Please let us know how it goes.

Hi,

On the default page layout the space the sidebar shows, but there is no actual sidebar. On the content left, sidebar right page layout the page displays as normal (with no sidebar and no change in layout).

I have corrected the login details so please let me know what you find :slight_smile:

Mike

Hi Mike,

I am a little confuse with your setup. I can see you are using Icon. To display Sidebar on Page, we don’t need the following code:

// 11. Give Support Centre Index The Support Sidebar Sidebar
// =============================================================================
// 11a. Assign Default Sidebar to Support Centre Index
add_filter('x_option_x_layout_content', 'sidebars_in_support_index', 5);
function sidebars_in_support_index($contents) {
  if ( is_page ( 'support-centre' ) ) {
    $contents = 'content-sidebar';
  }
  return $contents;
}

That is only applicable for post or custom post type because by default, page has Page Template option. We just need to set Content Layout: Content Left, Sidebar Right

That setup will now give you sidebar by default on pages that is using Default Page Template
Now to override that default sidebar on that page the code should be:

// 11b. Override Default Sidebar with Alternative for Support Centre Index
add_filter( 'ups-sidebar-support-sidebar', 'support_index_sidebar', 5 ); //testing 5
function support_index_sidebar($sidebar){
  if ( is_page ( 'support-centre' ) ) {
		return 'ups-sidebar-support-sidebar';
	}
	return $sidebar;
}

Compare the difference on the code.
Note to clear cache before checking.
Hope this helps.

Hi Lely,

Thank you. This is a big website and my default page layout is full width as I only want sidebar right on very few pages. Can I not override the pages rather than setting the default to sidebar right then manually going into every page to change the layouts?

Mike

Hi Lely,

I have followed your instructions above and there is no change. You can see the results on the page below and feel free to log in to the site to check my work.

Mike

HI there,

Could you try this code replacing all of the above,

add_filter( 'ups-sidebar-support-sidebar', 'support_index_sidebar', 5 ); //testing 5
function support_index_sidebar($sidebar){
  if ( is_page ( 'support-centre' ) ) {
		return 'ups-sidebar-support-sidebar';
	}
	return $sidebar;
}
  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_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 = 'full-width';
      } 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_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';
      }

    return $layout;

  

Then edit your /support-centre/ page and go to Page Attributes section and find the template selection, then select the template with sidebar (eg. Content Sidebar).

Thanks!

Hey @Rad,

I’ve tried this and it hasn’t worked.

I’ve included the exact (relevant) code used below to ensure that it’s correct, however, you have access to the site and you’re welcome to view it there (you can also do whatever you want to it in terms of trying to work this out, as it’s a temporary dev site).

I’ve also tried all combinations of site-wide default layouts and page attribute layouts with this code and still not working… I can’t believe this is so hard to achieve! Any more ideas?

// 10. Give Support Articles The Support Sidebar
// =============================================================================
// 10a. Assign Default Sidebar to Support Articles
add_filter('x_option_x_layout_content', 'sidebars_in_support_articles', 5);
function sidebars_in_support_articles($contents) {
  if ( is_singular( 'epkb_post_type_1' ) ) {
    $contents = 'content-sidebar';
  }
  return $contents;
}

// 10b. Override Default Sidebar with Alternative for Support Articles
add_filter( 'ups_sidebar', 'support_articles_sidebar' );
function support_articles_sidebar($sidebar){
	if( is_singular( 'epkb_post_type_1' )){
		return 'ups-sidebar-support-sidebar';
	}
	return $sidebar;
}


// APEX STAFF TEST CODE FOR SIDEBAR ON SUPPORT CENTRE PAGE AND SUPPORT CPT ARTICLES
// ==============================================================
add_filter( 'ups-sidebar-support-sidebar', 'support_index_sidebar', 5 ); //testing 5
function support_index_sidebar($sidebar){
  if ( is_page ( 'support-centre' ) ) {
		return 'ups-sidebar-support-sidebar';
	}
	return $sidebar;
}
  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_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 = 'full-width';
      } 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_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';
			}

		return $layout;
	}

Hi there,

I checked and it actually works, the problem is your page doesn’t have .x-main and .x-sidebar which is present in all X’s templates. And this means, your page isn’t using X template at all, or something is overriding it. And yes, the culprit is the FAQ plugin, that plugin has option for Template and you should choose Current Theme Templates Used for Posts and Pages instead of Knowledge Base Templates Designed for Articles. I went ahead and changed that and it now has a sidebar :slight_smile:

And because your current stack is ICON where special template is needed to use for sidebar, then I also added this few lines to the above code

 elseif ( is_page_template( 'template-blank-1.php' ) ) {
                $layout = 'content-sidebar';
      }

Thanks!

As always, absolutely fantastic support from you. All sorted and thank you very much to all who helped.

Glad to hear it.

Cheers!