Skip to content

Instantly share code, notes, and snippets.

@markbain
Created June 6, 2014 04:55
Show Gist options
  • Save markbain/2497190bc88e090bd0eb to your computer and use it in GitHub Desktop.
Save markbain/2497190bc88e090bd0eb to your computer and use it in GitHub Desktop.
Add a number to each widget in the sidebar.
function my_widget_class($params) {
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);
// Interpolate the 'my_widget_class' placeholder
$params[0]['before_widget'] = str_replace('my_widget_class', $class, $params[0]['before_widget']);
return $params;
}
add_filter('dynamic_sidebar_params', 'my_widget_class')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment