Different widgets on different page templates?

If I have a couple of different Page templates, how would I show a different collection of sidebar widgets for each of these templates? I’m using the Starkers theme as a starting point.

Related posts

Leave a Reply

5 comments

  1. You will need to create more sidebars in your functions.php file and then edit the page templates to call the sidebar you want.

    Adding sidebars

    Go in to your functions.php file. You should see some sidebars already being registered. The code will look something like this:

    //Adds default sidebar
     if ( function_exists('register_sidebar') )
     register_sidebar();
    

    To add another sidebar, add the following code any number of times after the existing sidebar registration.

    //Registers new sidebar
    if ( function_exists('register_sidebar') ) {
        register_sidebar(array('name' => 'Name Sidebar Here','before_widget' => '','after_widget' => '','before_title' => '<h2 class="widgettitle">','after_title' => '</h2>'));    
    } 
    

    Where it says ‘Name Sidebar Here’ put a logical name for this new sidebar. The rest of the array allows you to put HTML before the widget (before_widget) if your theme requires that for its design and put HTML after the widget (after_widget). Also, more commonly used in themes is a custom style for widget titles. You can put that HTML before the title (before_title) and after the title (after_title). In the example above, each widget title will have <h2 class="widgettitle"> placed before it and after it to close the opening tag.

    Add your new sidebar to your page templates

    Now that you’ve added a sidebar, you’ll need to put it in the page template where you want it. Find where the default sidebar is being called inside of your template ( usually ) and replace it with the following, where the number is the order of where the sidebar was added in functions.php file.

    <?phpif ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(Sidebar number here) ) : ?><?php endif; ?>
    

    This sidebar was the second one added in the functions.php file, so to call it in the page template, you’ll put 2 inside of !dynamic_sidebar(Put sidebar number here) ).

    Add widgets

    Once you’ve added it to the page template, just add widgets to the sidebar in your Appearance–>Widgets administration page. The new sidebar will appear there with the name you gave it in the functions.php file.

    Hope this helps!

    1. Widget Logic is one solution – a good one too.

    2. Multiple sidebars is the more robust solution since you can completely customize all the before/after _widget markup in functions.php. Not to mention that you can make any number of these sidebars.

    3. A more lightweight solution might simply be to use the body class to distinguish between types of pages on the css. Hide the stuff you dont want, display the stuff you do. Its a quick and simply way to getting that sort of thing to happen.