Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexkalh/cc57d43b4e8426e02389 to your computer and use it in GitHub Desktop.
Save alexkalh/cc57d43b4e8426e02389 to your computer and use it in GitHub Desktop.
Add first and last css classes for sidebar widgets wordpress
<?php
add_action('init', 'ak_add_order_classes_for_widgets' );
function ak_add_order_classes_for_widgets() {
global $wp_registered_sidebars, $wp_registered_widgets;
#Grab the widgets
$sidebars = wp_get_sidebars_widgets();
if ( empty( $sidebars ) ) {
return;
}
#Loop through each widget and change the class names
foreach ( $sidebars as $sidebar_id => $widgets ) {
if ( empty( $widgets ) ) {
continue;
}
$number_of_widgets = count( $widgets );
foreach ( $widgets as $i => $widget_id ) {
$wp_registered_widgets[$widget_id]['classname'] .= ' widget-order-' . $i;
# Add first widget class
if ( 0 == $i ) {
$wp_registered_widgets[$widget_id]['classname'] .= ' ct-widget-first';
}
# Add last widget class
if ( $number_of_widgets == ( $i + 1 ) ) {
$wp_registered_widgets[$widget_id]['classname'] .= ' ct-widget-last';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment