Add “first” and “last” CSS classes to dynamic sidebar widgets.
/**
* Add "first" and "last" CSS classes to dynamic sidebar widgets. Also adds numeric index class for each widget (widget-1, widget-2, etc.)
*/
function widget_first_last_classes($params) {
global $my_widget_num; // Global a counter array
$this_id = $params[0][‘id’]; // Get the id for the current sidebar we’re processing
$arr_registered_widgets = wp_get_sidebars_widgets(); // Get an array of ALL registered widgets
if(!$my_widget_num) {// If the counter array doesn’t exist, create it
$my_widget_num = array();
}
if(!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) { // Check if the current sidebar has no widgets
return $params; // No widgets in this sidebar… bail early.
}
if(isset($my_widget_num[$this_id])) { // See if the counter array has an entry for this sidebar
$my_widget_num[$this_id] ++;
} else { // If not, create it starting with 1
$my_widget_num[$this_id] = 1;
}
$class = ‘class=”widget-‘ . $my_widget_num[$this_id] . ‘ ‘; // Add a widget number class for additional styling options
if($my_widget_num[$this_id] == 1) { // If this is the first widget
$class .= ‘widget-first ‘;
} elseif($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) { // If this is the last widget
$class .= ‘widget-last ‘;
}
$params[0][‘before_widget’] = preg_replace(‘/class=\”/’, “$class”, $params[0][‘before_widget’], 1);
return $params;
}
add_filter(‘dynamic_sidebar_params’,’widget_first_last_classes’);
Search My Site
Categories
- Article (1)
- ASP.NET (1)
- Christians (1)
- Git (2)
- Instruction (5)
- iPhone (1)
- Just 4 Fun (3)
- Knowledge (58)
- Mobile Phone (1)
- Multimedia (18)
- My reflection (29)
- News (2)
- PHP (1)
- Uncategorized (2)
- Wordpress (22)
Archives
- April 2013 (3)
- February 2013 (2)
- January 2013 (4)
- December 2012 (2)
- September 2012 (4)
- August 2012 (1)
- July 2012 (4)
- June 2012 (2)
- May 2012 (4)
- April 2012 (4)
- March 2012 (2)
- February 2012 (6)
- January 2012 (1)
- May 2011 (6)
- April 2011 (2)
- March 2011 (2)
- January 2011 (1)
- December 2010 (1)
- September 2010 (3)
- August 2010 (10)
- July 2010 (2)
- June 2010 (3)
- May 2010 (7)
- April 2010 (2)
- March 2010 (7)
- January 2010 (7)
- December 2009 (10)
- November 2009 (6)
- October 2009 (11)
- August 2009 (15)
- July 2009 (12)
Blogs
Christians
My Community
Website
Top Posts
Recent Posts
Blog Stats
- 283,112 hits
Recent Comments