My sidebar widget code in functions.php looks like this…
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Home Sidebar',
'id' => 'home-sidebar-widget',
'before_widget' => '<div class="menu side %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="sidebarTitle">',
'after_title' => '</h4>',
));
Which creates this markup on the site…
<div class="menu side widget_text">
<h4>widget 1 title</h4>
<div class="textwidget">Lorem ipsum dolor sit amet</div>
</div>
<div class="menu side widget_text">
<h4>widget 2 title</h4>
<div class="textwidget">Lorem ipsum dolor sit amet</div>
</div>
Here’s what I need (just adding a number to the class collection)…
<div class="menu side s1 widget_text">
<h4>widget 1 title</h4>
<div class="textwidget">Lorem ipsum dolor sit amet</div>
</div>
<div class="menu side s2 widget_text">
<h4>widget 2 title</h4>
<div class="textwidget">Lorem ipsum dolor sit amet</div>
</div>
I would like to add a count variable so that each sidebar gets a number that I can then use for css targeting. How?
2 Answers
There doesn’t seem to be an easy way to do this. However, you can try this rather hackish approach:
add_filter('dynamic_sidebar_params', 'my_sidebar_params_cb');
function my_sidebar_params_cb($params) {
global $my_widget_counter;
if (empty($my_widget_counter)) $my_widget_counter = 1;
else $my_widget_counter++;
$params[0]['before_widget'] = str_replace('class="', 'class="widget_nr_'.$my_widget_counter.' ', $params[0]['before_widget']);
return $params;
}