When you need to remove the sidebar completely for a Custom Post Type (CPT) in WordPress. First you need to locate or create your template file and remove the call to get_sidebar().
However this can leave you with an empty space where the sidebar use to be. This is because of the CSS class sidebar
on your body
element. Removing this can be done by adding the following to your theme’s functions.php
file.
function alterBodyClasses($classes) { // Check if this page is our CPT e.g. Book if (is_singular('book')) { if (($key = array_search('sidebar', $classes)) !== false) { // Either replace with the opposite CSS class $classes[$key] = 'no-sidebar'; // OR unset the class completely // unset($classes[$key]); } } return $classes; } add_filter('body_class', [$this, 'alterClasses'], 99);
Comments are closed.