Skip to content

Instantly share code, notes, and snippets.

@mlbd
Last active February 14, 2018 08:12
Show Gist options
  • Save mlbd/8b8f51b175fc045d1adb1342c9b6b2d7 to your computer and use it in GitHub Desktop.
Save mlbd/8b8f51b175fc045d1adb1342c9b6b2d7 to your computer and use it in GitHub Desktop.
Add different class every second and third in WordPress Widget
/**
* Modify before_widget from Widget Class
*/
if ( !function_exists( 'pixicleaner_filter_dynamic_sidebar_params' ) ) :
function pixicleaner_filter_dynamic_sidebar_params($params){
static $sidebar_widget_count = array();
$sidebar_id = $params[0]["id"];
if ( $sidebar_id == 'pixicleaner_footer_widgets' ) :
if (! isset($sidebar_widget_count[$sidebar_id])){
$sidebar_widget_count[$sidebar_id] = 0;
}
$before_widget = $params[0]['before_widget'];
$class = ($sidebar_widget_count[$sidebar_id] % 4 == 1) ? "pixicleaner__spcial_widget col-md-2" : "";
$class .= ($sidebar_widget_count[$sidebar_id] % 4 == 2) ? "pixicleaner__spcial_widget col-md-2" : "";
$class .= " pixicleaner-in-$sidebar_id";
$before_widget = str_replace("class=\"",
"class=\"$class ", $before_widget);
$params[0]['before_widget'] = $before_widget;
$sidebar_widget_count[$sidebar_id]++;
endif;
return $params;
}
endif;
add_filter("dynamic_sidebar_params", "pixicleaner_filter_dynamic_sidebar_params");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment