Ryan Ball
Member
Posted 1 year ago #
Is there any way to add an additonal CSS class to the widget markup?
I've registered a new sidebar via the following snippet and I'd like to insert an additional CSS class: "widget-x" (x being the order/position of the widget within the sidebar). Any suggestions?
register_sidebar(array(
'id' => 'footer',
'name' => __('Footer'),
'description' => __('Widgets in this area will be shown witin the footer.'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
fadingdust
Member
Posted 1 year ago #
Here ya go:
function widget_index_class($params){
$sw=wp_get_sidebars_widgets();
$this_sidebar=$params[0]['id'];
$this_widget_id=$params[0]['widget_id'];
$widgets=array_flip($sw[$this_sidebar]);
$this_widget_index=$widgets[$this_widget_id];
$params[0]['before_widget'] = preg_replace( '/class="/', "class=\"widget-index{$this_widget_index} ", $params[0]['before_widget'], 1 );
return $params;
}
add_filter('dynamic_sidebar_params', 'widget_index_class');
Add the above to functions.php.. I've got it after the sidebars are registered.