Determine which sidebar to load
-
I’m starting to become a bit desperate.
I have a WordPress site with several pages.
All of these pages need to load their own specific sidebar.
Except for the sidebar, most of them are default page templates, so for obvious reasons I don’t want to write a seperate template for every page, with the only difference that they load another sidebar.So I’m looking for a way to create a ‘centralized’ way to pick the correct sidebar for the correct page.
I tried a few plugins, but these are ‘none-widgetized’ sidebars and they are ‘hard coded’ in the theme.What I did and what did not work:
I created a function in functions.php.
This function was called with the page id, and it returned a sidebarname.function pick_sidebar($pageid ) { switch ($pageid) { case 7: $sidebar = "menu"; break; case 10: $sidebar = "arrangements"; break; (etc. etc) } return $sidebar; }
and in the page.php I called the sidebar like this:
<?php $pageid = $wp_query->post->ID; $sidebar = pick_sidebar($pageid); get_sidebar($sidebar); ?>
But that failed. The page.php template just calls the default sidebar. It looks like get_sidebar ‘refuses’ to accept a variable as input.
(When I echo $sidebar from page.php, just before calling the sidebar, I can see that the function returns a correct value)Further I have put everything in a seperate php file which does everything by itself
($pageid = $wp_query->post->ID;, the switch statements and the get_sidebar() call)
And I just tried to include that in several ways, but that also fails.
a plain PHP include fails due to server restrictions which doesn’t allow to include via absolute paths (I used get_bloginfo to get the path to the template directory) and when I tried to include it with get_template_part it also fails.And finally I did a combination of the two, I put everyting (like in option 2) in a function in functions.php, but that just gives no results
The first option, with the function in functions.php, which returns the sidebarname, is my most favorite pick and maybe someone can tell me if it is possible (and how) to use a variable as input for get_sidebar.
But maybe someone has a much better idea about this?
- The topic ‘Determine which sidebar to load’ is closed to new replies.