Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
2  
What have you found in your research‌​? –  brasofilo Feb 25 '13 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 '13 at 17:39
    
Add a checkbox to the post edit screen? –  paul Feb 25 '13 at 19:29
add comment

6 Answers 6

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' );
share|improve this answer
add comment

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.

share|improve this answer
    
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 '13 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 '13 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 '13 at 18:10
add comment

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

share|improve this answer
    
I want the sidebar hidden on one post, not on all posts. –  ahockley Feb 25 '13 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 '13 at 18:57
add comment

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();
} 
share|improve this answer
add comment

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 here: https://raw.github.com/justintadlock/hybrid-core/master/extensions/theme-layouts.php

share|improve this answer
add comment

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.

share|improve this answer
    
The Plugin works with WP 3.5.1! –  drabello Feb 26 '13 at 6:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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