Different sidebar on static pages

Hi, Can someone help with how to get a different sidebar to show up by using conditional statements? Here is what I have that is not working:

I have registered three sidebars in widget.php for three pages I want to display its own respective sidebar. Then I placed this conditional statement in custom.php, but it is not working.

if (is_page(‘security-classes’)) {
  dynamic_sidebar(‘sidebar-security’);
} elseif (is_page(‘safety-classes’)) {
  dynamic_sidebar(‘sidebar-safety’);
} elseif (is_page(‘new-page’)) {
  dynamic_sidebar(‘sidebar-new’);

} else {
  dynamic_sidebar(‘sidebar-primary’);
}

Hi,

dynamic_sidebar is a template function and thus should be used in template file or hooked into an action that runs in a template file using add_action(). Putting that in functions.php or any file it imports does not tell wordpress where to echo the sidebar so it just does it whenever it loads that file. I could further explain this, but have a look at the WordPress Codex first then let me know if you have questions.

In your case you can replace the line in templates/sidebar.php with the following:

<?php
if ( is_page('security-classes') ) :  
  dynamic_sidebar('sidebar-security'); 
elseif ( is_page('safety-classes') ) :
  dynamic_sidebar('sidebar-safety');
elseif ( is_page('new-page') ) : 
  dynamic_sidebar('sidebar-new');
else: 
  dynamic_sidebar('sidebar-primary');
endif;
?>
1 Like

Hi, I must be doing something wrong–I’m not a PHP developer–it’s not working. Where exactly in sidebar.php? I’m using roots-classic and my sidebar.php starts like this:

class Roots_Sidebar {
private $conditionals;
private $templates;

public $display = true;

disregard–I edited the wrong file, DUH! thanks enollo.

I am glad it is working now. It was my pleasure to help.

We all have to start somewhere. Not too long ago all of us were in the same boat, and we will keep learning for the remainder of our careers. Happy thought! :smile:

2 Likes

I don’t think this code works entirely. Roots’ Primary sidebar gets appended to the bottom of any of the other sidebars you’ve specified in the sidebar.php file. Are you getting this result as well?

UPDATE

Never mind, you win. My code was a little off.

This helps me lot! Thanks!

I thought adding some additional info might help others here. Before I do, I’d just like to say Enollo did a spot on job explaining this, thank you sir!

SO quickly to apply multiple sidebars to the roots theme goes something like this:

  1. Register Sidebar in widgets.php

  2. Call sidebar in template file(i.e. base-new-page.php) using snippet of code from Enollo:

    <?php if ( is_page('security-classes') ) : dynamic_sidebar('sidebar-security'); elseif ( is_page('safety-classes') ) : dynamic_sidebar('sidebar-safety'); elseif ( is_page('new-page') ) : dynamic_sidebar('sidebar-new'); else: dynamic_sidebar('sidebar-primary'); endif; ?>

3.Add content to the sidebar. Be sure you do this or it will only appear as though your default sidebar is loading.

Because Roots is so bad ass it’s as easy as that!

I have no idea why but in my case I had lots of different sidebars and one of them was called “sidebar-news”.

That “sidebar-news” was crashing down everytime the whole sidebar.php file.
Once I renamed it to something else everything started working fine. (conditional logic etc everything!)