Hiding Sidebar On Certain Page

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:

  1. The Page's selected Page Template
  2. page.php
  3. 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. [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.

Further reading:

http://codex.wordpress.org/Pages

http://codex.wordpress.org/Pages#Page_Templates

http://codex.wordpress.org/Conditional_Tags

About Len Kutchma

Len has been blogging for over 10 years and is a rabid WordPress fan. In addition to blogging here you can find him writing the occasional article and toiling away in the forums at WeblogToolsCollection.com. He also hangs out at the WordPress support forums lending a hand where he can. Be sure to follow @wpcanada on Twitter.

Comments

  1. Ismail says:

    When comes to WordPress, in future I need to refer to you as my 'guru" :)

  2. Len Kutchma says:

    Like I said earlier, I'm no guru. Just another WP user who is addicted to the software. :)

    • Judy says:

      Len,

      I have been trying to get rid of the sidebars on WordPress Essence page for over a week. Following your instructions for creating a new page template and deleting the sidebar call worked. I had tried to do this before but the new template did not show in the Page Attributes template dropdown list.
      Thanks for the help.

  3. Pammy says:

    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.

  4. Pammy says:

    Oops, system took out my code. Anyway it is called with an include TEMPLATEPATH command.

  5. Len Kutchma says:

    Hi Pammy. What theme are you using? I'll have a look.

  6. Karen says:

    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?

  7. Len Kutchma says:

    Hi Karen,

    Exactly what are you trying to accomplish and what theme are you using?

  8. Mike says:

    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.

  9. Mike says:

    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

  10. Len Kutchma says:

    Glad to see you figured it out Mike. :)

  11. Ashley says:

    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?

  12. Len Kutchma says:

    Hi Ashley,

    What theme are you using?

  13. Saijo George says:

    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 ?

  14. Saijo George says:

    Figured out how to do it :)
    It can be done using is_page()

    more info : http://codex.wordpress.org/Conditional_Tags#A_PAGE_Page

  15. Len Kutchma says:

    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. :)

  16. mobilex says:

    thanks for this post.. was trying to hide the sidebar from google search results..

  17. Len Kutchma says:

    Glad you found the info useful. :)

  18. Worlo says:

    Hi,

    Thanks for your advice, but how can I hide the sidebar on different pages.

    Using your example below I have tried to hide the sidebar on pages 2,8 16 but it will not work.....

    Any tips or advice for a newbie?

  19. Len Kutchma says:

    Hi Worlo,

    Which method are you using as I outlined 2 different ways - I suspect the 2nd. If so you need to use a "elseif" statement if you want to exclude more than one page.

  20. Worlo says:

    Hi Len,

    Correct the 2nd option. I tried using the elseif statement outlined below but it would not work for me..... any thoughts on where I am going wrong ?

    I did not include "" in the below example as the code reason did not appear in my last post for some reason.

    php if (is_page('2,8,16')) :
    php else :
    php get_sidebar();
    php endif;

  21. Len Kutchma says:

    Hi Worlo,

    Try adding this to the very top of page.php

    <?php get_header(); ?>
    <?php if (is_page('2')):
    elseif (is_page('8')):
    elseif (is_page('16')): ?>
    <?php else : ?>
    <?php get_sidebar(); ?>
    <?php endif; ?>

    This will only work if those pages are using page.php. If you have built custom template files to display those pages you'll have to go with the first option.

  22. Worlo says:

    Hi Len,

    Thanks very much. Your code did excatly what I wanted. Excellent.

    Another question for you - sorry!

    When I select a nav button from the main nav menu at the top of the site the nav button selected changes colour and stays this colour until another main nav bar button is selected which is exactly what I need.

    Now my side bar only appears when the "About Us" main nav bar button is selected. However when ever I select a link from the side bar the "about us" button on the main nav bar no longer has a different background colour.

    In short what I am trying to do is when any of the links on the side bar is selected, I would like to the "about us" button on the main nav bar stay a different colout to show the user that they are still within the "about us" section....

    Any thoughts on how I can do this???

  23. Len Kutchma says:

    Hmm, I'm not sure I understand. You have a page called About Us? What link in the sidebar are you clicking. I suspect this link takes you to somewhere other than About Us.

  24. Worlo says:

    Hi Len,

    I have a horizontal nav bar at the top of my site. When I click on "about us" which is a nav button on the horizontal nav bar the sidebar appears. Based on your advice above the side bar only appears when the "about us" pages is selected.

    When I click on a page link from the side bar menu, I would like the "aboutus" nav button to stay shaded to let people know they are still within the "about us" section of the site......

    Any thoughts...

  25. Len Kutchma says:

    If you're referring to "dynamic highlighting" you may want to take a look at this post.

    How To Build Horizontal Menu

    In it I describe how to use dynamic highlighting.

  26. steve says:

    thank you for this information, it helped me greatly!

  27. Len Kutchma says:

    You're welcome Steve.

  28. steve says:

    Len, I'm hoping you can help me with an issue that I'm running into. I went with your second option to take out the sidebar on a particular page. There is another page which I need to do so. How do I go about adding multiple pages to that second technique in order to kill the sidebar?

    A add-on to that question is the page I need the sidebar taken out of, has a built in "sidebar-projects" section which I would like to keep.

    here is the code in my page.php file

    ID, $key, true) != 'wide')&&(get_post_meta($post->ID, $key, true) != 'project-view') )
    {
    get_sidebar();
    }
    elseif (get_post_meta($post->ID, $key, true) == 'project-view')
    {
    include ('sidebar_projects.php');
    }
    ?>

    I would appreciate any input.
    Thanks,
    Steve

  29. steve says:

    I'm sorry, not all the code came over

    ID, $key, true) != 'wide')&&(get_post_meta($post->ID, $key, true) != 'project-view') )
    {
    get_sidebar();
    }
    elseif (get_post_meta($post->ID, $key, true) == 'project-view')
    {
    include ('sidebar_projects.php');
    }
    ?>

  30. Len Kutchma says:

    Hi Steve.

    To do this for multiple pages you would need to use an elseif statement. For example,

    <?php if (is_page('123')) : elseif (is_page('456')); ?>
    <?php else : ?>
    <?php get_sidebar(); ?>
    <?php endif; ?>

    You can add as many pages as you like by using the elseif statement. With respect to your second question I'm not sure I understand. Perhaps it might be easier simply to create a new page template for that page then call that specific sidebar in it.

  31. steve says:

    ugghh, I'm not sure why all the code is not coming over, please delete my two previous posts. I have figured out how to hide the sidebar on, one page, and saw in the comments above how to hide it on two, but on some specific pages I have a template where there is a sidebar_projects.php that I need to have shown. Any idea on how to accomplish that?

    Thank you.

  32. Len Kutchma says:

    I would just create new page templates for those pages and call that sidebar with them.

  33. Dennis says:

    I used the second option. The first one doesn't work, when adding a new page it won't let me choose which template to use. I'm running WordPress on my own webserver.

  34. Len Kutchma says:

    Hi Dennis,

    It's odd that the first option doesn't work as it's a built-in WordPress feature. Glad to see you got it going with the 2nd option.

  35. Mena says:

    Hi,

    I have this theme http://www.webdesignlessons.com/dynablue-wordpress-theme/

    And i can't find anything as mentioned above in the page.php file,

    can you help me please ?

    Mena’s last blog post..للناسِ أعماقٌ أشدُ خطورةً من البحار!!!

  36. Len Kutchma says:

    Hi Mena.

    I had a quick look at that theme. The sidebar is actually called in footer.php. I'll have a closer look tomorrow when I have more time.

  37. Mena says:

    Okay, Thank you very much.

    Mena’s last blog post..mysqlerror

  38. You're cool. I was following your instruction and figured out that the template I'm using iNove has an alternate page titled "page without sidebar". It gives me the option to use this template in the drop down under template.

  39. Len Kutchma says:

    Glad you got it all sorted out Nick. :)

  40. Len Kutchma says:

    You're welcome Mena.

  41. Len Kutchma says:

    Mena,

    This theme will require ALOT of work to achieve the result you want because it uses a series of background images. You would have to create a new series of graphics.

    Having said that, to get rid of the sidebar on certain pages we do so by creating a new Page Template and an alternate footer.php file.

    First of all, create a new Page Template which can be an exact copy of your theme's page.php. For sake of simplicity let's call it nosidebar.php although you can call it whatever you want. At the very top of nosidebar.php insert the following ...

    <?php
    /*
    Template Name: No Sidebar
    */
    ?>

    Next, make a copy of your theme's footer.php and call it myfooter.php (again, it doesn't matter what you call it) In the newly created myfooter.php delete the following ...

    	<div id="body_right">
            <div id="sidebar_ads"><?php if(function_exists('theme_ads_show')) theme_ads_show(); ?></div>
    		<p class="adv_here"><a href="#" rel="nofollow">Advertise Here</a></p>
    		<div id="sidebars">
    
                           <?php get_sidebar(); ?>
    
    		</div>
    	</div>

    Next, go back to nosidebar.php and look for <?php get_footer(); ?> and replace it with <?php include (TEMPLATEPATH . '/myfooter.php'); ?>

    So, what we have done is create a new Page Template which calls a new footer file. When you write a new page (or edit an existing one) you should be able to select the new Page Template from the drop down menu. That new Page Template will not call footer.php but WILL call myfooter.php which omits the call to the sidebar. However it will look a tad strange. You can see what I mean by clicking here.

    As I said earlier, the reason for that is this theme uses a series of background images, namely bgr_body.gif, bgr_body_end.png and bgr_body_top.png. All of those images would have to be recreated and the style sheet adjusted for the calls to the new images. This means ALOT of work. You're probably better off with finding a new theme.

    Anyway, you can find copies of the files you'll need at the WordPress Pastebin. Just click the links.

    nosidebar.php
    myfooter.php

  42. Mena says:

    Thank you very much, maybe i will use another themes, actually lately i'm between many themes and trying this and this, and wish to find out a good one, maybe i will create my own after learning wordpress codex although i tried before with no result

    But the way you have mentioned is very cool, i really loved it.

    Thank you agian,
    Mena

    Mena’s last blog post..

  43. Len Kutchma says:

    You're welcome Mena. Good luck.

  44. dtjb says:

    Just another comment of praise – this was exactly what I was looking for. Worked like a charm.

    Thanks!

  45. Len Kutchma says:

    Glad it worked for you!

  46. Andy says:

    Thanks for the tip! You put me on the right track!

  47. Len Kutchma says:

    You’re welcome Andy.

  48. Amit Cohen says:

    Hi
    Thanks for the “easy to do” tutorial.
    That is great.
    If you like, you can see how it looks here: http://rummycity.com/gin-tutorial/

    Each template have different code for the sidebars, so just pay more attention to the code you deleting.
    Beside that, it’s really good and useful.

    Amit.

  49. Len Kutchma says:

    Nice work Amit. :)

  50. Gouri says:

    After breaking my head for 3 days trying to create a google custom search page, I had finally given up. Today, again I thought, why not give it one more try. I googled for removing sidebar, came across your post, got the funda clear and finally set it up.

    When you said “This is not as difficult as it sounds”, it really built up my confidence.

    Your writing style is simple and informative. It makes my conviction strong – SIMPLICITY RULES.

    Thanks a lot, keep rocking!!

    …while I move on to your about page.

  51. Len Kutchma says:

    Glad I could be of help Gouri. By the way, I tested your custom search page – nice work. :)

  52. Tory mcBroom says:

    Sorry for asking this question on an old post, but i want to remove the sidebar from a specific post using option 2, so would I just do this in my singlepost.php file:

    And my next question is how do you get the content area to fit the whole page using option 2. i don’t want the blank white space where the sidebar was.

    Thanks for your help!!

  53. Len Kutchma says:

    Hi Tory, no need to apologize. That’s the reason why I haven’t closed replies on older posts. Some of them still generate alot of interest. I would have gotten back to you sooner but I had a hernia attack Monday morning and I’ve been in agony since. Hopefully it will be over soon. :)

    Anyway, on to your question. (and what a great question) I hadn’t thought about doing this for a particular post. Obviously Page Templates won’t work here as WordPress will be using single.php.

    I haven’t tested this code but it should work. First, you want to set up 2 different classes or IDs. You can take a look at the Kubrick theme to see how this works. Notice how that theme uses the class “narrowcolumn” for index.php and the class “widecolumn” for single.php?

    Look in the single.php for the call to the sidebar. It will look something like…

    <?php get_sidebar(); ?>

    In place of that we insert our first condition as such…

    <?php if (!is_single('x')) : get_sidebar(); endif; ?>

    What that says is if we are on single.php and viewing the post with the ID of x (you’ll have to insert the numerical ID of the actual post there) do nothing ELSE get the sidebar. So far so good.

    Now we have to create another conditional. This all depends on how your theme is structured. I’ll use the generic “content” ID. Look in single.php for…

    <div id="content">

    and replace with the following,

    <div id="<?php if (!is_single('x')) : ?>content<?php else : ?>contentwide<?php endif; ?>">

    Basically what that says is if we are on single.php and viewing the post x (where x is the numerical ID of the actual post) we switch the ID content to the ID contentwide. Then you simply go in to your style sheet and set your definitions for #contentwide

  54. Gouri says:

    Len, Get well Soon. :)

  55. Len Kutchma says:

    Thanks Gouri. After nearly a week I’m starting to feel better. :)

  56. Virg says:

    Hello! Great post!

    In regards to the blank space where the sidebar used to be, you can just delete the "div id" of the column used for your sidebar (directly from your new page template) and that will widen your content.

    Tho' I'm nOt sure if that'll work on all themes!

  57. Len Kutchma says:

    Hi Virg, thanks for stopping by. Yeah depending on how the theme is constructed it may not work.

  58. faizan says:

    hi all!
    i have installed simple press forum plugin in my blog. the forum page has still sidebar at left. please help! i need to get forum page rid of sidebar and increse forum width!

    cheers faizan

  59. Mike says:

    I just wanted to thank you and say that the edited option worked for me...conditional tags were simple to use.

    Thanks!

  60. Van says:

    Hey Len,

    I am trying to follow your steps however my page.php file does not have the "get sidebar" code. Instead, I found it in the footer.php file. How can I make it work?

    Van

    • Van says:

      I actually figured that out through your comments above, now I have a different question:

      How can I achieve that for a certain post? I'm not quite sure since the get_sidebar is in my footer.php

      Hope you can help me.

      Van

      • Len Kutchma says:

        Hi Van

        Sorry for the delay in replying. The roofing season is in full swing and I'm swamped at work. Not much time for online fun. :)

        Could you give me a link to the theme you're using so I can have a look at it?

  61. Colin says:

    Hi Len,

    I've used this method successfully, but for some reason I can't get it to work for multiple pages...only a single page. I think I'm coding it wrong.

    I did this:

    So basically I wanted to remove the sidebar on page "x" and page "y". How do I go about doing that, since the comma isn't working.

    For the record, I can block just page "x" using this method. I figured a comma separating pages would work.

    Thanks so much!

    Colin

  62. Colin says:

    It removed the code from my previous comment, but the code I was referring to was listed right below:

    "... and add a few lines of code around it so that it now looks like this ..."

    • Len Kutchma says:

      Hi Colin

      For multiple pages you will need to use an elseif statement. Have a look at my earlier comment to Steve and see if that helps.

      • Colin says:

        FYI, I think you need the ";" to be a ":" on the first line for the code to work properly.

        • Len Kutchma says:

          I'm not following Colin. Referencing the comment I linked to above, I am using a :

          Did I slip up somewhere else? I can't find it.

  63. Colin says:

    Aahh....Should have scanned the comments better.

    Thank you for your easy to understand directions, much appreciated!!

    • Len Kutchma says:

      This particular post has generated a lot of comments. It's easy to overlook something. :)

  64. Hi Len,

    Had been looking for a way to remove the sidebar. Thanks for the useful info. I think I will go for the second option you posted as it seems a bit simpler.

  65. Gloopy says:

    Len top post but what i need is a little bit different, what if i had a forum e.g

    http://www.gloopy.co.uk/forum/

    is there a simple way to exclude all of the pages after the /forum/ because doing these manually is not appropriate. I am only a newbie so please don't laugh if this is a silly question.

    I read somewhere that adding $ represents all files in a certain category but not sure.

    • Len Kutchma says:

      Hi Gloopy,

      Not sure I understand the question. You only need to create one Page Template then assign the applicable pages to use it as opposed to using the default page.php template file.

Please Note: WPCanada is a moderated community. Please read the Comment Policy.

HAVE SOMETHING TO SAY?

*