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.
Here’s an idea I mentioned on Twitter.
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:
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 classpostid-{post-id}
will be applied to the body. You can use this to your advantage.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.
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.
For reference: http://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page
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 likepost=xxx
.xxx
is yourpost id
)Then change your single template like this
Lets say your post id is
66
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
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
, pastesingle.php
content and add the following code in line 1:3 – Remove the
<?php get_sidebar(); ?>
fromnosidebar-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.
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:
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