WordPress: How to Remove the Sidebar from a Single Page

January 6th, 2010 | 17 comments

You may come upon the situation where you need/want to remove the sidebar for one particular page on a WordPress bog. There are many situations where this may happen. In one case, if you use Google Custom Search (within Adsense) for your blog search and you want the search results to match your blog (highly recommended) the minimum width for the results page is 795 set by Google. That means, with most themes, the ads on the right in the search results are going to overlap your sidebar.

So, here is the solution:

WARNING: Always make a backup of any file that you edit, no matter what it is you’re editing or who is giving you the advice!

Open page.php in your favorite editor, or even in notepad.

Replace the following line:

< ?php get_sidebar(); ?>

With this:

< ?php if (is_page('X')) : ?>
< ?php else : ?>
< ?php get_sidebar(); ?>
< ?php endif; ?>
< ?php get_footer(); ?>

Replacing the x with the page number. To find the page number, login to WordPress, go to pages and hover your cursor over the page you want to edit. Then look at the status bar of your browser and look at the URL. At the end of the URL you’ll see post=(the number)

Next, add the following code directly after the closing php tag ?> in your theme’s functions.php file:

< ?php
add_action('wp_head','wpi_custom_page_styles',11);

function wpi_custom_page_styles(){

// http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page
$pid = 'refi-google-search';

if (is_page($pid) ): ?>
<style type="text/css">
/* full width 926px + 25px (left &amp; right) padding */
#content{ width: 901px }
</style>
< ?php endif; } ?>

That’s it.

offline internet marketing


Related Posts

Leave a Comment with Facebook

17 comments

  1. M Z - Traffic Guide
    6th January, 2010 at 5:44 pm 

    Hey, this is also useful for when you want to write your own sales page and don’t want any distracting links.

    Great to know that you don’t need to mess with the css (time consuming).

  2. Todd Morris (6 comments.)
    8th January, 2010 at 4:24 pm 

    Hi Josh,

    Instead of using conditional statements, and editing functions.php, wouldn’t it be easier to just create a new page template that doesn’t have the sidebar?

    Many themes already include this option. And if they don’t, a new page template can be made in about 10 minutes.

    The only reason I ask is that I’d be kind of leary about suggesting that anybody who doesn’t know what they’re doing edit functions.php. (lol, try putting an extra comma in there and see how quick you’ll get an error message at the top of your blog).

  3. Josh Spaulding
    8th January, 2010 at 4:30 pm 

    @ Todd – Possibly, I don’t know. This is the solution I used, so I thought I would share it. How exactly would you create a new page template from within WordPress? Nothing wrong with editing any file. That’s why we do backups and that’s why I used a big “WARNING” up there.

  4. Todd Morris (6 comments.)
    8th January, 2010 at 6:19 pm 

    @Josh,

    I agree about making a backup .. it’s just the functions.php can be very finicky, as compared to some other files.

    As far as creating a new template:

    Copy your page.php file, and rename it. In the case of your example something like search_result.php.

    Then instead of using those conditional statements, just remove “php get_sidebar();”

    Finally, find the div ID for your “content” area, and rename it something like “content-nowidget”.

    Edit your style.css to add #content-nowidget with attributes you’ve defined above. (width: 901px)

    Upload the template to your wp-content/themes/yourtheme folder.

    Then, when you write a new page, there’s a drop down menu on the right labeled “templates”. Just choose the one you made.

    The biggest advantage to doing it this way, is that it’s much, much easier if you later want multiple pages on your site that have no sidebar.

    Just create a new page, and choose from the drop down again.

    Hope this helps,
    Todd

    p.s. you can also use this method to make other “custom” templates for your pages, and soon you’ll also be able to do the same thing with posts.

  5. Josh Spaulding
    8th January, 2010 at 6:49 pm 

    Hey Todd, thanks for the info. The way I did it was quick and easy, so that’s the way I’ll stick with, but now people have another choice, so I appreciate that.

  6. Josh Spaulding
    8th January, 2010 at 10:45 pm 

    Hey Todd, I’m giving it a shot. But no luck finding the templates menu you refer to. Are you sure it’s there in the newer version of WP?

  7. Todd Morris (6 comments.)
    9th January, 2010 at 3:43 am 

    Hi Josh,

    Sorry, it was night over here in Iraq, I was sleeping :-)

    The menu is on the right hand side, under “Attributes”. It’s a drop-down menu that should say “Default Template” right now.

    I’m using WP 2.9.1

  8. Jay | Niche Volumes (1 comments.)
    10th January, 2010 at 6:05 am 

    Thanks for this very useful tip. One of the themes I’m using has different page templates and even has one without the sidebar.

    May I add for clarity that the ‘templates menu’ can be found on the right hand side when editing a page.

  9. Josh Spaulding
    11th January, 2010 at 4:49 pm 

    ahhhh ok I was looking at a post, not a page and that option doesn’t show up on posts. Looks like a great option for pages. Now we need an easy option for doing that with posts.

    Where in Iraq are you? I stayed at Victory for several months, Taji and a several other spots in 03. You guys have Internet, buildings etc. now. It’s just not right lol

  10. Todd Morris (6 comments.)
    11th January, 2010 at 4:58 pm 

    Hi Josh,

    Apparently post templates will be coming in WP 3.0.

    As for Iraq, I’m down at Talil in the south, this time. In 07, I was way up north at Tal Afar. Did also take a trip to Victory when I first got here in Oct. It was pretty cool, we took a break from our conference, and toured a couple of bombed out palaces.

    Things are relatively quiet here these days. And yes, for most people, it’s definitely not a “rough” deployment. Going to Fort Irwin for NTC was much worse … and that only lasted a month.

    Anyhow, I’m about to call it a night again … think we’re about 9 hours ahead of you guys.

    Talk to you again soon,
    Todd

  11. Joe
    14th January, 2010 at 1:41 pm 

    Thanks Josh, this great for landing pages. I have been using a theme with no side bars, now I can use any them I want.

  12. John (1 comments.)
    17th January, 2010 at 12:58 am 

    Rather than replacing/removing lines why not just comment them out? So you wont have to backup any files (although a good a idea either way) and you can revert the changes easily by just removing the comments.

  13. Andy Beard
    21st January, 2010 at 11:25 am 

    Note: There are a number of plugins that allow you to use post templates in the same way as page templates

  14. Linda (1 comments.)
    30th March, 2010 at 2:07 am 

    thanks for this Josh. Will come in handy when I want to create a sales page for my site for the first product I create

  15. Curious Sense
    27th April, 2010 at 4:37 am 

    I do not have the drop-down menu to choose a template on my Edit page. I’m using WP version 2.9.2.

    Any ideas?

  16. John Burnside (4 comments.)
    16th August, 2010 at 1:43 pm 

    Thanks for the post. Really appreciate it when people put how to do basic things in code on the internet becuase it really not my thing so these step by step instructions really help.

  17. Ron - SEO Sales Copywriting | Web Content Writing | Blogging (8 comments.)
    24th February, 2011 at 11:36 am 

    Oh! I was looking for this. Though you can always create a separate page in HTML/CSS anytime. It doesn’t harm to learn more.

Leave a reply

© 2011 Dot Com Solutions, Inc. All Rights Reserved. Syndication is not authorized without consent.


Disclosure Statement | Privacy & Disclaimer