4

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.

3
  • 1
    This is a duplicate, you already have a very similar question runnung: wordpress.stackexchange.com/questions/805/… - if you did not get the answer you were looking for there, please edit your original questions and make it more precise.
    – hakre
    Aug 24, 2010 at 16:53
  • The question is a little bit different
    – cannyboy
    Aug 24, 2010 at 17:24
  • All the answers are the same as the other post though
    – Dan Gayle
    May 26, 2011 at 22:45

5 Answers 5

8

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!

3

And Widget Logic is also an option.

1
  • Please note that this plugin open a wide security risk by using PHP eval function. Sep 19, 2013 at 13:55
1
  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.

1

An another solution is Display-Widgets plugin, it adds a dropdown menu in every widget you have activate in your sidebar/s where you can select in which pages/posts to show or hide it.

0

two other options: 1. instead of making 'sidebar.php' more complicated add the new widgetized sidebars to a new sidebar file 'sidebar-xxx.php' and then in the page template use

  1. you can use contextual-widgets plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.