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.
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:
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;
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!
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?
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:
Register Sidebar in widgets.php
Call sidebar in template file(i.e. base-new-page.php) using snippet of code from Enollo:
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!)