Problem:
I need to have dynamic classes for widgets. Let's suppose I have a single sidebar. Which widgets and the number of widgets displayed is unknown (this is for a public theme).
I'm trying to make the widgets appear something like this:
<div id="widget-id" class="widget-class widget-1 widget-odd">
Widget stuff
</div>
<div id="widget-id" class="widget-class widget-2 widget-even">
Widget stuff
</div>
<div id="widget-id" class="widget-class widget-3 widget-odd">
Widget stuff
</div>
Why?
This would allow for alternating styles and other cool CSS stuff by specifically referencing widgets by class. I can't reference a particular widget because there's no way for me to know what widget a user would be using.
What I have thus far:
I have the basic widget setup:
register_sidebar(array(
'name' => '__('Widget Name','localization'),
'before_widget' => '<div id="%1$s" class="%2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="insert-header insert-title">',
'after_title' => '</h3>',
)
);
I wanted to add another class like this by referencing a function to generate the dynamic class:
'before_widget' => '<div id="%1$s" class="%2$s' . my_widget_class() . '">',
Normally, for something like posts or comments, I could do something like this, but it won't work in this situation:
function my_widget_class() {
global $widget_num;
// Widget class
$class = array();
$class[] = 'widget';
// Iterated class
$widget_num++;
$class[] = 'widget-' . $widget_num;
// Alt class
if($widget_num % 2) :
$class[] = 'insert-even';
else :
$class[] = 'insert-odd';
endif;
// Join the classes in the array
$class = join(' ', $class);
// Return the class for use in the widget
return $class;
}
I'm wondering if there's some kind of filter I'm overlooking that would handle this. Basically, I could just use a push in the right direction.