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
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. [demo currently unavailable]
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.