How can I hide the sidebar from a specific post?

I’d like to hide the sidebar on a specific post (not page). How can I do this?

For background, I’ve searched quite a bit both on Google and here. I’m finding lots of solutions for showing different sidebars on posts vs. pages, or for showing (or hiding) a specific sidebar on a specific page, but nothing that’s about hiding the sidebar from a single specific post.

Related posts

Leave a Reply

7 comments

  1. Here’s an idea I mentioned on Twitter.

    function special_post_template( $template ) {
      // Identify a single query for the special post
      if ( is_single() && 10 == get_the_ID() )
        $template = get_template_directory() . '/special-single.php';
    
      return $template;
    }
    
    add_filter( 'template_include', 'special_post_template' );
    
  2. One potential approach isn’t so much “hiding” a sidebar as it is not including it.

    Pages

    Every page in your site can use a different template. Usually, you’ll just create a template that uses the default (“Default Template”) that includes the header, footer, content, sidebar(s) as you’d normally expect. However, you can create a new page template for your site that follows the same formatting, but omits the sidebar.

    Alternatively, if you you know the ID or slug of the page in question, you can create a page template just for that pace. Just place one of the following files on your theme:

    • page-{id}.php
    • page-{slug}.php

    WordPress will load it by default.

    Posts

    For posts, tho, WordPress doesn’t support this kind of hierarchy. You can get around it by creating a custom post type, then creating single-{post-type}.php to display that post type without a sidebar, but that might not be what you want.

    However, if your theme is applying appropriate body classes, then you can filter based on the post ID once again and use CSS to hide things. When properly using body_class() in your theme, the class postid-{post-id} will be applied to the body. You can use this to your advantage.

    /* ... All of your other CSS rules */
    
    body.postid-15 .sidebar {
        display: none;
    }
    

    The downside with this approach is that the sidebar markup is still loaded, it’s just hidden with CSS. But for now (until WordPress implements a per-post template similar to the per-page one it already has), this should be enough to get you started.

  3. If you need the solution for only one post, then get the post id from your admin panel.

    (If you hover over edit link, you will see something like post=xxx. xxx is your post id)

    Then change your single template like this

    Lets say your post id is 66

    if (!is_single(66)) {
       get_sidebar();
    } 
    
  4. If you need this on an ongoing basis, I recommend the Theme Layouts extension from Justin Tadlock. You can define the layouts you want. It’ll give you a radio box on each post so you can specify the layout you need for each.

    It’s easy to do if you know WordPress theming. Include the extension, add_theme_support for theme-layouts, and you’re on the way.

    It’s included in his Hybrid Core framework, or you can see the Theme Layouts extension classes here: https://github.com/justintadlock/hybrid-core/tree/master/inc

  5. In my opinion a better way is to work with post templates.

    In that case, to hide/remove the sidebar from a specific post follow these steps:

    1 – Open your theme post template, probably single.php, select all and copy the content;

    2 – Create a secondary post template, i.e. nosidebar-posts.php, paste single.php content and add the following code in line 1:

    <?php
    /*
    Template Name Posts: No Sidebar Posts
    */
    ?>
    

    3 – Remove the <?php get_sidebar(); ?> from nosidebar-posts.php or edit as you like;

    4 – Install the Custom Post Template Plugin;

    5 – Go to edit post and you will notice a Post Template Box, select the No Sidebar tpl and you’re done.

    Now you can remove the sidebar from any post by simply selecting the template in your edit post page and you can create multiple templates, like full width, no sidebars etc.

  6. If you are on WordPress 4.7 or greater, you can apply the page templates on blog posts as well. Just add post as Template Post Type.

    On your page template, define it as follows:

    <?php 
    /*
    Template Name: Full-width layout
    Template Post Type: post, page
    */
    ?>
    

    Now on post editor window,you should see Page Attributes section showing that template.

    Reference: https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-page-templates-for-specific-post-types