Archived topic
Removing sidebar on certain CPTs
3 replies · Started by Matthew on November 8, 2021
Hi, weird problem. Our site has several different CPTs and I found a post here in the forum on how to hide the sidebar on select CPTs by adding a filter to functions.php: https://generatepress.com/forums/topic/removing-sidebars-from-custom-post-types/
The problem I have is that this solution only works on the first CPT in the array, Guides. Customers still have a sidebar. Below is the exact code I added to functions.php and here is a link to an example single Customers page: https://manyrequests.wpengine.com/customers/benjamin-williams-55knots/
add_filter( 'generate_sidebar_layout', 'df_cpt_sidebar_layout' );
function df_cpt_sidebar_layout( $layout ) {
$post_types = array( 'guides', 'customers' );
if ( in_array( get_post_type(), $post_types ) ) {
return 'no-sidebar';
}
return $layout;
}
I also tried creating a second filter with a new array yet that made no difference -- the Customers sidebar remained. What can I do to hide the customer sidebar? I tried hiding in via CSS which worked at first, until I noticed the alignfull class no longer works because the body no longer has the no-sidebar class anymore. (That CSS has been removed.)
And yes, I can 100% confirm that the slug is spelled correctly. There are several other filters that reference the 'customers' $post_type and all work correctly, so we can eliminate it being a slug typo.
Completely out of ideas now. The CPT settings, the templates, everything about these two CPTs are identical yet for some reason we just cannot get rid of that darn sidebar. Any suggestions?
Hi there,
just an aside before we take a look at the code... have you tried using a Layout Element ?
https://docs.generatepress.com/article/layout-element-overview/
Hi David,
Thank you, I didn't think about that. Yes, that works. Doesn't explain why the php didn't work but whatever, the sidebar is gone and alignfull works as intended now, so let's take the win.
Appreciate the quick response, have a great day!
Yeah thats a peculiar one - can't see why that code isn't working. It looks correct.
Glad to hear the element approach worked though!