Hide Sidebar From A Custom Post Type (CPT) In WordPress

If you’re looking to hide a sidebar from a custom-post-type (CPT) in WordPress, you can make use of the following code:


function ob_no_sidebar( $class ) {

	// Alter your layout
	if ( is_singular( 'job_listing' ) ) {
		$class = str_replace("has-sidebar","",$class);
	}

	// Return correct class
	return $class;

}
add_filter( 'body_class', 'ob_no_sidebar', 20 );

This is something I created for the custom-post-type job_listing in WP Job Manager plugin. This code basically adds a body_class filter and the runs my function ob_no_sidebar.

My function will check if the current post type matches XYZ then it’ll replace the has-sidebar class with an empty string and that does the job.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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