Advertisement
alchymyth

widget stuff

Jul 25th, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. /**
  2.  * Add "recurrent numbered" CSS classes to dynamic sidebar after widgets .
  3.  * adapted from a code by  @MathSmath /
  4.  * http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets?replies=9
  5.  */
  6. function after_widget_number_classes($params) {
  7.  
  8.     global $my_widget_num; // Global a counter array
  9.     $this_id = $params[0]['id']; // Get the id for the current sidebar we're processing
  10.     $arr_registered_widgets = wp_get_sidebars_widgets(); // Get an array of ALL registered widgets 
  11.  
  12.     if(!$my_widget_num) {// If the counter array doesn't exist, create it
  13.         $my_widget_num = array();
  14.     }
  15.  
  16.     if(!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) { // Check if the current sidebar has no widgets
  17.         return $params; // No widgets in this sidebar... bail early.
  18.     }
  19.  
  20.     if(isset($my_widget_num[$this_id])) { // See if the counter array has an entry for this sidebar
  21.         $my_widget_num[$this_id] ++;
  22.     } else { // If not, create it starting with 1
  23.         $my_widget_num[$this_id] = 1;
  24.     }
  25.  
  26.     $class = 'class="separator-' . (($my_widget_num[$this_id]%3)?$my_widget_num[$this_id]%3:3) . ' '; // Add a widget number class for additional styling options
  27.  
  28. $params[0]['after_widget'] = preg_replace('/class=\"/', "$class", $params[0]['after_widget'], 1);
  29.     return $params;
  30.  
  31. }
  32. add_filter('dynamic_sidebar_params','after_widget_number_classes');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement