1

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.

3
  • 4
    What have you found in your research?
    – brasofilo
    Feb 25, 2013 at 16:58
  • @brasofilo I've found lots of info on how to show a different sidebar on pages, or on how to show different sidebars on posts vs. pages, but nothing that applies to hiding the sidebar on a single specific post. Unless you have found something that I haven't...
    – ahockley
    Feb 25, 2013 at 17:39
  • Add a checkbox to the post edit screen?
    – paul
    Feb 25, 2013 at 19:29

7 Answers 7

3

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' );
1

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
  • What about some form of meta data? This way the css will not have to be updated if pages/posts ids change.
    – nvwd
    Feb 25, 2013 at 17:59
  • Page/post IDs shouldn't change unless you're resetting your DB and keys. And if you're doing that, then there's enough other stuff going on that this would be a much smaller concern.
    – EAMann
    Feb 25, 2013 at 18:02
  • Let's try different reasoning... If you want to specify which posts don't get the sidebar, meta data would allow it to be set on a post by post basis. Then the template can look at the meta data to make the decision.
    – nvwd
    Feb 25, 2013 at 18:10
1

Couldn't you just tap into the is_single() conditional? Or are you unable to touch your single.php file. If you can, I'd try out an if statement for your post.

if(is_single('potato-pancake')) {
   // nothing
} else {
   get_sidebar();
} 

For reference: http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page

2
  • I want the sidebar hidden on one post, not on all posts.
    – ahockley
    Feb 25, 2013 at 18:22
  • Actually, that's what that code would do. If you're viewing a single post with that slug, do nothing. Otherwise (i.e. all posts) show the sidebar.
    – EAMann
    Feb 25, 2013 at 18:57
1

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();
} 
1

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

0

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.

1
  • The Plugin works with WP 3.5.1!
    – drabello
    Feb 26, 2013 at 6:00
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.