The smart way to disable your sidebar

If you find yourself making a new WordPress template for pages that are identical to others on your site but just don’t have a sidebar, consider this technique instead which lets you turn the sidebar off selectively on a page by page basis.


<?php // look to see if we've disabled sidebar in a custom field, if not show it
	$disableSidebar = get_post_meta($post->ID, 'disableSidebar', $single = true);
	if ($disableSidebar !== 'true') { get_sidebar(); }
?>

what it does

It checks to see if you have a custom field set in your post to disable the sidebar and if you do it turns off the sidebar

how to use it

Just make a new custom field for your page called disableSidebar and set it to true. That’s it.

« previous postnext post »

46 Comments

  • http://www.pankajbatra.com Pankaj Batra

    Thanks!

    It worked well

  • Scott

    where do you put the code snippet?

    • http://www.mimoymima.com mimoymima

      It replaces get_sidebar(); in any of your templates

  • http://slick-sites.com/services/ Luis Clark

    Thank you so much,really works !

  • http://www.weboracle.com.au/ Web Oracle

    Let me try this tweak and see what happens. Thanks.

  • Jeff

    Works great. Thanks!

  • kevin

    Thank you very much!

    Was looking for this code for a while!

  • Victor Ndiema

    Thanks for the code, but suppose I would like to remove only a single sidebar either left or right, how do I tweak the code.
    Thanks.

    • http://www.mimoymima.com mimoymima

      That's pretty easy, you'd just change all the instances of disableSidebar to disableSidebarLeft or disableSidebarRight or disableSidebarWhatever, and then in the custom fields you make on the page/post you'd set disableSidebarRight to true to disable that one specifically. If the page doesn't have the specific custom field in listed in the code the sidebar will show up. Hope that helps.

  • irene

    Thnaks so much for great tip.

    One more help, if you don't mind.

    When I do this, i need the wide of main column also change to cover the empty space of sidebar are.

    Any possible way to do this? Thanks in advance.

    • http://www.mimoymima.com mimoymima

      If you float left (or right) your sidebar and give it a width but don't give a width to your content area (it defaults to width: auto;) then the content should take up the extra space when you remove the sidebar.

  • irene

    i'been change the width of content column to auto.

    Result; the width of content change to full (same as i wish for).

    However, for the posting that enabled sidebar area, the content also full width and the sidebar area move to down of the content.

    I have 3 column for my magazine theme.

    Main content in the left column, and 2 sidebars in the right column.

    Here is my style.css;

    — insanely long stylesheet removed here —

    ============================

    I'm not so familiar with css, any help would be appreciate.

    Once again, thanks for your help.

    • http://www.mimoymima.com mimoymima

      Hey Irene, If your sidebar is wrapping below the content when you have the content area set to width: auto; there could be any number of things causing it… Maybe google 'three column css layout' and try some of the tutorials out there for making a good css based three col layout.

  • MiK

    Thank You! Using the same way i managed to disable the title :)

  • http://www.jamesemeryphoto.com James

    Which file exactly does this go on? page.php?

    • http://www.mimoymima.com mimoymima

      it can go into any template, page.php, index.php, a custom template… wherever you'd normally put a sidebar in your code…

  • http://www.byebedbug.com allen

    I put the code in my the index and page.php files but when I create a field disablesidebar and set it to true nothing happens

    • http://www.mimoymima.com mimoymima

      did you replace the code that normally calls in your sidebar with our code? if you just add it in addition to your other code you'd probably get two sidebars… Are you sure you have the custom field exactly as the example? it's case sensitive…

    • http://www.mimoymima.com mimoymima

      it's disableSidebar not disablesidebar (case sensitive)

  • http://www.cruisebugvacations.com Tess

    I am using the Web Minimalist Theme. I have tried pasting this code in multiple places and it does not seem to affect anything. It appears that all sidebar calls are in the stylesheet.

    Do you have any suggestions?

    I have added the custom field as indicated above.

    Thanks for any suggestions that you may have. I appreciate it.

    • http://www.mimoymima.com mimoymima

      You need to replace the code that is in your theme that calls your sidebar with this code. Sidebar calls aren't in the stylesheets they're in the php template files.

      • http://www.cruisebugvacations.com Tess

        Hi,

        That is my problem. This theme does not have a template.php. The page.php does not call the sidebar.

        The only files where I see sidebar mentioned are the Stylesheet and functions.php, which has this code: <?php

        if ( function_exists('register_sidebar') )

        register_sidebars(2);

        OH there is also a sidebar.php

        I am going crazy trying to figure this out. I think that the theme (Web minimalist) is strange or I am just very stupid. Any further suggestions greatly appreciated. Thanks.

  • katie

    The idea here is that you will modify your page.php file to check for a “disable sidebar” indicator (aka a custom field) for each page. Then, anytime you want to create a page with no sidebar, you simply set a custom field to indicate that you wish to disable the sidebar for that specific page.

  • James

    Hi, I need help please.

    The theme I am using allows you to choose whether you want one sidebar or two, and also which side of the page you want the sidebar.

    This is the current call code

    When I replace it with this

    ID, 'disableSidebar', $single = true);

    if ($disableSidebar !== 'true')

    $loc = theme_option('sidebar_location');

    if($loc==2 || $loc==4) {

    get_sidebar(); // calling the First Sidebar

    }

    if(theme_option('sidebar_width2')!=0 && $loc!=3) get_sidebar( "second" ); // calling the Second Sidebar

    ?>

    I remove the sidebar from the page that I want it removed, but on the pages where I want it to remain the sidebar stays on the left side of the page even when I select for it to be on the right.

    Can someone please help me with this.

    Thank you

    James

  • James

    Hi, I need help please.

    The theme I am using allows you to choose whether you want one sidebar or two, and also which side of the page you want the sidebar.

    This is the current call code

    <code></code>

    When I replace it with this

    <code>ID, 'disableSidebar', $single = true);

    if ($disableSidebar !== 'true')

    $loc = theme_option('sidebar_location');

    if($loc==2 || $loc==4) {

    get_sidebar(); // calling the First Sidebar

    }

    if(theme_option('sidebar_width2')!=0 && $loc!=3) get_sidebar( "second" ); // calling the Second Sidebar

    ?></code>

    I remove the sidebar from the page that I want it removed, but on the pages where I want it to remain the sidebar stays on the left side of the page even when I select for it to be on the right.

    Can someone please help me with this.

    Thank you

    James

    • http://www.mimoymima.com mimoymima

      I don't see this anywhere in your code:

      <code>$disableSidebar = get_post_meta($post->ID, 'disableSidebar', $single = true);</code>

      That's the part that looks for a custom field and turns off the sidebar, I'm guessing thats further up in your code?

      Other than that I don't think I can help, there's too much going on in your theme for me to know what's wrong. Normally if you have multiple sidebars you'd wrap each with a different conditional statement using different variable names like $disableSidebarLeft and $disableSidebarRight.

      Good luck

  • http://z-beautyLounge.com Charles

    Please,I am soo novice that i need help with how to make a new custom field for your page called disableSidebar and set it to true to start with.

    Thanks

    • http://www.mimoymima.com mimoymima

      one of the boxes when you are making a new post or page is for adding custom fields, if you don't see it that means you probably have it turned off in the screen options. Click the little button on the top right of your screen that says 'screen options' and make sure it's checked, that's probably the problem.

  • rob nelson

    I love the idea of this condition = true stopping the side bar from coming up. I have pasted the code in index and page ,php. And set my condition correctly but the side bar is still there.

    I am using the Chip Zero Theme.

    Thanks for any help you can provide.

    • http://www.mimoymima.com mimoymima

      there's not anything in the code that would conflict with a certain theme, the only thing I can think of is that your theme is registering and calling a specific sidebar and not just 'sidebar' so whatever code you had in your theme for calling the sidebar would need to be used, so in the code below, where it says get_sidebar(); that might need to be something more specific, like get_sidebar('name-of-sidebar'); you'd see this name in the call to the sidebar, that you are replacing, also if you replaced it already you could look at functions.php to see what sidebars are registered…

      <code>
      <?php // look to see if we've disabled sidebar in a custom field, if not show it
      $disableSidebar = get_post_meta($post->ID, 'disableSidebar', $single = true);
      if ($disableSidebar !== 'true') { get_sidebar(); }
      ?>
      </code>

  • http://www.food-recipe-tips.com Alan

    Hi I'm trying to remove the sidebars from my search page. I'm using Genesis, when i go into wp-admin->appearance->editor, I looked into all the php files, but there isn't a "get_sidebar" entry.

    Would appreciate any help. Thanks.

    • http://www.mimoymima.com mimoymima

      could be that it's using the dynamic_sidebar(); function, if so follow the same steps above but just replace get_sidebar with dynamic_sidebar. Let me know if that works for you.

      • http://www.antsmagazine.com nahid

        Hi, Your code is working on my site. I have few other things to ask. The excerpt what I write is not spreading but stays where it was. I would like to set it to fluid so that whenever I do not have a sidebar then the excerpt should take the place of the sidebar and spread wide. I am doing this on a photography post. Where I would put big photos. So please let me know if I can do this anyway.

        Regards

        Nahid

        • http://www.mimoymima.com mimoymima

          Hey Hahid,

          This has to do with the CSS in your layout. For instance if the content area has a px width instead of being set to auto this would happen… You'd need to make a few changes to the CSS. What's the link to your site where I can see the problem?

        • http://www.antsmagazine.com nahid

          My site is http://www.antsmagazine.com.

          This piece of code works for the single.php i.e single post.

          #content { float:left; background:#fff; width:640px; margin:20px 0px 0px 0px; padding:0px 0px 15px 0px; }

          U can see the width set to 640px.. Now tell me how do I set it to float over sidebar area when there is not sidebar and narrow down to regular width when there is sidebar.

          http://www.antsmagazine.com

          i am sorry if this comment is repeated…

        • http://www.mimoymima.com mimoymima

          it's an easy fix actually, just change the width: 640px; to width: auto;

          I just tested it in firebug and looks like it works.

  • http://www.antsmagazine.com nahid

    wow that was quick. Thank you for the advice. How much do you charge for a wordpress design? Please give me your rates, I get wordpress projects.

    Regards

    Nahid

  • James

    Really helpful, I have no experience with wordpress and this has now made my theme worth keeping. Very thankful!

  • http://suburbansitters.com Blake

    I have a very basic theme with no custom fields. I installed "More Fields" to have custom fields, but I'm not sure how to use them.

    I want to remove the sidebar from my homepage only (static page on WordPress) and I do NOT have the <?php get_sidebar()> code in ANY of my theme files. Hence, why I am having such a problem.

    I love how easy this code looks, but I do not know where to put the code to make it work.

    suburbansitters.com is the site.

    Any help is greatly appreciated!

    Blake

    • http://mimoymima.com mimoYmima

      you must have get_sidebar somewhere or you wouldn't have a sidebar on the page. Is it all that stuff on the bottom of your home page you're talking about? that does look like a sidebar of sorts, maybe your home page uses a custom template like home.php

  • Lalit

    On Which file do I need to insert or add above code?

    Lalit from Tricky Planet

  • esty

    hello, I want to remove a side bar on one of my wordpress pages and only on one one page and I am afraid to put the remove sidebar code because I do not want to put it in the wrong place and remove all the side bars. where should I go and what should I put in to remove only on that specific page

    • b

      That’s what this code does, you put it around your sidebar call, then on just the pages you want the sidebar removed you set the custom field.

      • Victor

        my god! what’s a custom field?
        What’s it look like?
        Where to put it?
        How to create it on WordPress?
        Is it a plugin?
        Does it placed on the editing page of wp-admin?

        • http://mimoYmima.com/ Brent Lagerman

          no plugin necessary Victor, custom fields are just part of WordPress, if you don’t see them on your page or post you can click the ‘screen options’ button at the top of your screen and turn them on.