Support » Fixing WordPress » 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";
    case 10:
    $sidebar = "arrangements";
    (etc. etc)
    return $sidebar;

    and in the page.php I called the sidebar like this:

    $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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Are you using the correct names for your sidebar templates?

    Should be:


    Yes, that’s how I named them
    If I call them directly: <?php get_sidebar(“menu”); ?>
    Than everything works. (filename sidebar-menu.php)

    But when I do:

    $pageid = $wp_query->post->ID;
    $sidebar = pick_sidebar($pageid);
    echo "Sidebarname: ".$sidebar;
    get_sidebar($sidebar); ?>

    this results in:
    Sidebarname: menu
    And the default sidebar is displayed.

    Well using the WordPress Default 1.6 theme (the old Kubrick theme) I created a sidebar-menu.php template file (with a difference from sidebar.php) then in the index.php file changed

    <?php get_sidebar(); ?>


    <?php $menu='menu';
    get_sidebar($menu); ?>

    and it used the sidebar-menu.php file.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Determine which sidebar to load’ is closed to new replies.