Devin Walker is a San Diego based entrepreneur, developer and designer.

Roots Theme: Display Sidebar Only for Certain Pages – Default Full Width

Posted on Jul 2, 2013 in Coding | 7 Comments

roots-themeThe Roots Theme for WordPress is awesome for creating great websites. I’ve built many of the sites in my portfolio on Roots and actually have one showcased in the Roots Gallery (Vets2Go). One situation I come across every so often is the need to reverse the logic of the Roots sidebar.

Roots Sidebar Explanation

Roots has conditional checks that if return true won’t show the sidebar. So, for instance take a look at the standard roots_display_sidebar function in config.php:

All the arguments below instruct the Roots core to not display the sidebar.php file. We want to reverse this logic so that the arguments will display the sidebar.

This is because our certain template needs to be full-width according to the design and functionality. No sidebar, unless I tell it so in the arguments above. Make sense?

Reverse the Sidebar Logic

If we take a gander into the Roots core we will find the Roots_Sidebar class in lib/sidebar.php:

Change public $display = true; to public $display = false; on line 17 in the file.

Next, change $this->display = false; to $this->display = true; on line 27.

Jump Back Into Config.php

Now that our logic is reversed we need to update our arguments to display the sidebar only on the pages we wish:

Now only pages that use template-sidebar.php will display our sidebar. This is exactly what I need for a full-width layout with some pages using a sidebar.

Anything Else?

Hopefully this solves your issue with the Roots sidebar full-width by default problem. Check out this thread over at the Roots Google group for more information into the source of this information. Thanks to Catherine Azzarello and iamboris for first uncovering the problem and solving it with this elegant solution.

7 Comments

  1. Michael W Martin
    July 26, 2013

    Well presented!

  2. Gersande
    August 14, 2013

    Oh very nice.

  3. William Minty
    October 13, 2013

    Thanks for this man, this was exactly what I was after!

  4. KJ
    May 16, 2014

    thanks!

  5. rtpHarry
    January 30, 2015

    I know this was written a long time ago and it might have been before Roots supported this but I’m just posting this here for people still reading it today.

    Read http://roots.io/the-roots-sidebar/ and scroll down to “Filtering the Sidebar Class”.

    Roots now provides a built in mechanism for flipping it around to the scenario you describe above.

  6. randzgonz02
    November 26, 2015

    Thank you! This gave me an idea on how to remove the sidebar on specific pages by adding extra line inside setup.php(display_sidebar function). 🙂