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.

Leave a Reply

Add a new comment
38 Responses to “The smart way to disable your sidebar”

  1. Pankaj Batra says:

    Thanks!
    It worked well

  2. Scott says:

    where do you put the code snippet?

  3. Luis Clark says:

    Thank you so much,really works !

  4. Web Oracle says:

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

  5. Jeff says:

    Works great. Thanks!

  6. kevin says:

    Thank you very much!
    Was looking for this code for a while!

  7. Victor Ndiema says:

    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.

    • mimoymima says:

      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.

  8. irene says:

    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.

    • mimoymima says:

      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.

  9. irene says:

    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.

    • mimoymima says:

      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.

  10. MiK says:

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

  11. James says:

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

  12. allen says:

    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

  13. Tess says:

    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.

    • mimoymima says:

      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.

      • Tess says:

        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.

  14. katie says:

    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.

  15. James says:

    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

  16. James says:

    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

    • mimoymima says:

      I don’t see this anywhere in your code:

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

      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

  17. Charles says:

    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

    • mimoymima says:

      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.

  18. rob nelson says:

    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.

    • mimoymima says:

      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…


      < ?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(); }
      ?>

  19. Alan says:

    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.

    • mimoymima says:

      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.

      • nahid says:

        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

        • mimoymima says:

          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?

        • nahid says:

          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…

        • mimoymima says:

          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.

  20. nahid says:

    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


Enable Javascript