Hiding Sidebar On Certain Page
Filed Under: Blogging, WordPress
How to remove sidebar on 1 page, and keep it on the others? This was a question asked in the forums the other day and upon answering it I thought to myself what interesting fodder for another post! I supplied the original poster with 2 methods on how to achieve the result but in this post I’ll concentrate on the 2nd option.
In WordPress, a Page is used to display information other than regular posts and it lives outside of the normal blog chronology. In the default Kubrick theme there are 3 Page Templates that can be used to display the content of a Page:
- page.php - Default Page Template, displays Page content
- archives.php - ignores Page content and instead displays a list of Archives
- links.php - ignores Page content and instead displays your links
So which Template does WordPress use to display the content of a particular Page? WP will search your theme’s files for templates and use the first one it finds in this order:
- The Page’s selected Page Template
- page.php
- index.php
Now you probably don’t want to remove the sidebar from page.php because that will remove it from ALL Pages using that Template. So, we simply create a new Page Template and assign it to the Page that you don’t want the sidebar to appear in. This is not as difficult as it sounds. Simply create a duplicate of your theme’s page.php but leave out the call to the sidebar. In the default Kubrick theme the tag that calls the sidebar is …
<?php get_sidebar(); ?>
My theme is a little different. I have 2 sidebars and they are called as …
<?php include (TEMPLATEPATH . '/second.php'); ?>
<?php include (TEMPLATEPATH . '/third.php'); ?>
… but we’ll stick to using the first tag. Open up your theme’s page.php and copy/paste the contents into a new file. Leave out the tag that calls the sidebar. Using the example provided by the Codex, place this at the very top of your new file …
<?php
/*
Template Name: Snarfer
*/
?>
… and save it as snarfer.php. Upload the new Template to your theme directory. Now, when you create a new Page you simply assign your new Page Template - in this case snarfer.php - to display its content.
Of course after doing this you may want to make further adjustments since you will now be left with a strange looking empty space where the sidebar used to be. You will probably want to widen the content area so that if fills that empty space. All you need to do is rename the DIV that holds your content in your new Page Template then add its styling attributes to your style sheet. You can see how this is done by studying the differences between index.php and single.php in the default Kubrick theme. Notice how one uses class=”narrowcolumn” and the other uses class=”widecolumn”.
If you want to see a live example of this in action click here.
Update: After reading this post I thought perhaps that I would include the other option I provided in the forum for the benefit of anyone reading this in the event you don’t see that forum post. This option for removing the sidebar from a certain Page involves the use of conditional tags. Look in your theme’s page.php for the sidebar call. Once again, using Kubrick as an example, that would be …
<?php get_sidebar(); ?>
… and add a few lines of code around it so that it now looks like this …
<?php if (is_page('x')) : ?>
<?php else : ?>
<?php get_sidebar(); ?>
<?php endif; ?>
… where “x” is the ID number of that Page. What this says is “if we are on page “x” do nothing and if not get the sidebar”. While this method may be easier I prefer creating a new Page Template. Anyway, you now have 2 methods to hide your sidebar on certain Pages.
Further reading:
http://codex.wordpress.org/Pages
http://codex.wordpress.org/Pages#Page_Templates
http://codex.wordpress.org/Conditional_Tags
Thanks for visiting! If this is your first time here why not grab the RSS feed. You can also follow this discussion by checking the "Notify me of followup comments via e-mail" box or by subscribing to the comments RSS feed. Please scroll down to leave your comment.



15 Comments so far. Join the Discussion!
Ismail
When comes to Wordpress, in future I need to refer to you as my ‘guru”
Apr 14th, 2008
Len Kutchma
Like I said earlier, I’m no guru. Just another WP user who is addicted to the software.
Apr 14th, 2008
Pammy
My site template has the get_sidebar commands in the footer. It is called with:
How would you recommend making it go away on my forum.php page
is_page_template(’forum.php’)
I am barely a novice at PHP.
May 22nd, 2008
Pammy
Oops, system took out my code. Anyway it is called with an include TEMPLATEPATH command.
May 22nd, 2008
Len Kutchma
Hi Pammy. What theme are you using? I’ll have a look.
May 23rd, 2008
Karen
Hi! This was very helpful to me. The only problem I have is renaming the DIV in order to adjust the width. I don’t know how to do that. Can someone point me in the right direction?
Jul 31st, 2008
Len Kutchma
Hi Karen,
Exactly what are you trying to accomplish and what theme are you using?
Aug 3rd, 2008
Mike
I am trying to use your second method as I am a novice. I did your steps and it almost worked but I am left with the original width. How do I change that for one page? This is the page I am stuck with at the moment.
http://mikecandoit.com/?page_id=309
Thanks for the help.
Aug 5th, 2008
Mike
I actually figured it out. Often the simple solution is where it is. I was trying so hard to figure how to change the setting of the page and then realized when I have pictures too large they overlap the sidebar. So I changed the embed code from 100% to 900px. Simplify, simplify. But thanks to your code I figured it out.
Mike
Aug 5th, 2008
Len Kutchma
Glad to see you figured it out Mike.
Aug 5th, 2008
Ashley
Hello there. I’ve a bit of a problem concerning removing my sidebar from my template. You see, I don’t have the get_sidebar function in my template. The theme I’m using apparently calls a dynamic sidebar for the pages, and I’ve no clue how to get rid of it for this one page. Any help?
Aug 6th, 2008
Len Kutchma
Hi Ashley,
What theme are you using?
Aug 6th, 2008
Saijo George
Gr8 article , I will keep this as the last option for what I am trying to achieve.
What I really want to do is disable Adsense ads shown in the sidebar from a certain page and show some other ad in place of that. Do you recon that this is possible ?
Sep 10th, 2008
Saijo George
Figured out how to do it
It can be done using is_page()
more info : http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
Sep 10th, 2008
Len Kutchma
Hi Saijo
The beauty of dynamic code such as PHP and the way it works with WordPress is you can achieve almost anything. Glad to see you figured it out.
Sep 10th, 2008
Reply to “Hiding Sidebar On Certain Page”
Please note: Comment moderation is in effect. It may take some time for your comment to appear. There is no need to resubmit it.
Off-topic comments, personal attacks, obvious spam and support requests will likely not make it out of moderation. This site does not necessarily endorse or agree with comments left here.