Wrote a guide, as the process seemed mildly convoluted.
Here’s how to modify the _s (“underscores”) WordPress theme to support different sidebars that trigger depending on the page being viewed. There are plug-ins for this, but some of them are shady or just break the UI.
(E.g., Custom Sidebars notably made it impossible for me to save widget content, because its donation bar’s shitty CSS prevents users from being able to scroll far enough down to hit the button. Good times!)
Anyway, here’s the run down:
Overview
Edit your functions.php to register more sidebars.
Edit your sidebar.php file to throw in conditionals to determine how you invoke dynamic_sidebar().
Edit the corresponding widgets.
Edit your functions.php to register more sidebars.
1 2 3 4 5 6 7 8 9 10 |
|
Registering additional sidebars in functions.php is pretty straightforward. Here’s an example of how to register a second sidebar with a semantically meaningful name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
I was initially tempted to use register_sidebars() instead, but it would lead to a bunch of sidebars named by number (e.g., sidebar-1, sidebar-2, etc.) which would be a maintenance hazard in that the names would lack any sort of semantic meaning, forcing you to look things up. A stitch in time, and all that jazz.
Edit your sidebar.php file to throw in conditionals to determine how you invoke dynamic_sidebar().
1 2 3 |
|
Brief note for the uninitiated: that hideous ‘: … endif’ pairing is a PHP-idiomatic replacement for everyday curly-braces {} when the stuff between braces would have otherwise contained embedded HTML. (Yuck.)
1 2 3 4 5 6 7 8 9 |
|
The key here is nesting ifs (or using a case statement) to intelligently exploit WordPress’s conditional tags to only show a sidebar appropriate to the type of content being displayed. Said tags can be used to differentiate between pages, the blog index, categort pages, and so on, all the way down to even the individual post level. You’ve got a lot of control.
Warning
Be really cautious, here. Using things that otherwise seem they’d make sense can blow up in your face. Notably, using !dynamic_sidebar(‘sidebar-a’) || !dynamic_sidebar(‘sidebar-b’) will behave unexpectedly. Swapping &&, in the previous statement, for || does too.
Edit the corresponding widgets.
Open the WordPress admin panel. Appearance -> Widgets has a pretty obvious interface that will let you set up what gets displayed in each widget.
Bam! A little hand-waving later, and you’re good to go.