Sidebar on Specific Pages

Note: This is mentioned here: Using the filter for roots_display_sidebar. Thought I might expand on it a bit and add to the Example section.

This is for those of you who might be wondering how to display the sidebar on specific pages. More specifically you might be thinking, “I have a bunch of page and I only want a sidebar on a page or two!”

If you’ve done a little digging you’ll have realized that the function roots_display_sidebar() in lib/config.php allows you to do this through blacklisting (pages you don’t want a sidebar on) instead of whitelisting (pages that you want a sidebar on).

For example, if you had the following pages: Home, Products, Services, Contact, About and only wanted a sidebar on the Contact page. You’d have to do it like this:

// Conditional tag check
array(
     'is_404',
     'is_front_page',
     // all pages but "Contact" page
     array( 'is_page', array( 'Home', 'Products', 'Services', 'About' ), 
     'is_single',
);

Not a big deal for a small site that has little or no chance of the client/user adding pages to the site at some point in the future.

This might not be too enticing to those who have a lot of pages and only want a sidebar on a few pages. Especially, if by default, you don’t want a sidebar on a page. This is where the default implementation gets in the way. With every page added, you need to update the blacklist in the roots_display_sidebar() function with the new page.

One solution I have seen is to modify two values in the Roots_Sidebar class, located in the lib/ directory, setting public $display = false; and $this->display = true;. This switches everything around. You’d be whitelisting instead of blacklisting. This solution is simple and gets the desired result. For me, it feels a little dirty even though it is completely acceptable as the developers of Roots have plainly stated in Roots 101:

Since Roots is a starter theme, it’s okay for you to modify files within lib/ to meet the needs of the site you’re building.

Now, onto the solution.

There is an apply_filters() function that is hooked to roots_display_sidebar at the end of the roots_display_sidebar() function. Take advantage of this by creating a function in custom.php and add a filter to this hook. Something like:

function {theme_name}_display_sidebar( $sidebar_config ) {

     if ( $sidebar_config->display ) :
          if ( is_page('Contact') ) : // list of pages with a sidebar
               return true;
          endif;
     endif;

     return $sidebar_config;

}
add_filter( 'roots_display_sidebar', '{them_name}_display_sidebar' );

and in the roots_display_sidebar() function go about your business as usual.

// Conditional tag check
array(
     'is_404',
     'is_front_page',
     'is_page',
     'is_single',
)

In the end you initially say, “don’t add a sidebar to any page (via the roots_display_sidebar() conditional tag check of is_page).” Then through the filter hook roots_display_sidebar you say, “hey wait! I want the sidebar on this page(s)!”

2 Likes