How can I get the number of widgets that are active on a specific sidebar? Is there a core function for this?
I want to add a class to each widget on a sidebar based on how many of them are displayed.
Thanks.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
After some research and based on the answer from Eugene Manuilov I made a function that adds custom classes to widgets in a specific sidebar (‘sidebar-bottom’ in my case) based on the number of widgets set in that sidebar. This will suit perfectly in horizontal sidebars and themes based on twitter bootstrap that need spanX class to adjust the element’s width.
function cosmos_bottom_sidebar_params($params) {
$sidebar_id = $params[0]['id'];
if ( $sidebar_id == 'sidebar-bottom' ) {
$total_widgets = wp_get_sidebars_widgets();
$sidebar_widgets = count($total_widgets[$sidebar_id]);
$params[0]['before_widget'] = str_replace('class="', 'class="span' . floor(12 / $sidebar_widgets) . ' ', $params[0]['before_widget']);
}
return $params;
}
add_filter('dynamic_sidebar_params','cosmos_bottom_sidebar_params');
Method 2
Also, if you’re looking to do things to the first and last widget, you can use the code below. I took nautilus7’s code and combined it with MathSmath + durin’s code over at http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets to target a specific sidebar, add span classes based on the number of widgets in the sidebar, and then add a custom class to the first and last widget of the groups.
function cur_target_sidebar_add_classes_to_params($params) {
global $my_widget_num; // Global a counter array
$sidebar_id = $params[0]['id'];
if ( $sidebar_id == 'sidebar' ) {
$registered_widgets = wp_get_sidebars_widgets();
if(!isset($registered_widgets[$sidebar_id]) || !is_array($registered_widgets[$sidebar_id])) { // Check if the current sidebar has no widgets
return $params; // No widgets in this sidebar... bail early.
}
$number_of_widgets = count($registered_widgets[$sidebar_id]);
$rounded_number_of_widgets = floor(12 / $number_of_widgets); //Rounds number of widgets down to a whole number
if(!$my_widget_num) {// If the counter array doesn't exist, create it
$my_widget_num = array();
}
if(isset($my_widget_num[$sidebar_id])) { // See if the counter array has an entry for this sidebar
$my_widget_num[$sidebar_id] ++;
} else { // If not, create it starting with 1
$my_widget_num[$sidebar_id] = 1;
}
$classes = 'span' . $rounded_number_of_widgets;
if($my_widget_num[$sidebar_id] == 1) { // If this is the first widget
$classes .= ' first-widget ';
} elseif($my_widget_num[$sidebar_id] == count($registered_widgets[$sidebar_id])) { // If this is the last widget
$classes .= ' last-widget ';
}
$params[0]['before_widget'] = preg_replace('/class="/', 'class="' . $classes . ' ', $params[0]['before_widget'], 1);
}
return $params;
}
add_filter('dynamic_sidebar_params','cur_target_sidebar_add_classes_to_params');
Method 3
function wpse_54162_get_widgets_count( $sidebar_index ) {
global $wp_registered_sidebars;
$index = "sidebar-{$sidebar_index}";
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
return 0;
return count( (array) $sidebars_widgets[$index] );
}
Method 4
Lets try with below code
function sidebar_widget_init()
{
global $_wp_sidebars_widgets;
if ( empty( $_wp_sidebars_widgets ) ) :
$_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
endif;
$sidebars_widgets_count = $_wp_sidebars_widgets;
$sidebar_count = count( $sidebars_widgets_count[ 'sidebar3' ] );
if($sidebar_count == 2){
$sidebar_class = 'class_name1';
} elseif($sidebar_count == 3){
$sidebar_class = 'class_name2';
} elseif($sidebar_count == 4){
$sidebar_class = 'class_name3';
}
register_sidebar(array( 'name' => __( 'Sidebar Footer', 'templatemesh' ),
'id' => 'sidebar3',
'description' => __( 'Widgets in this area will be shown on footer Sidebar.', 'templatemesh' ),
'before_title' => '<h3 class="widget_title">',
'after_title' => '</h3>',
'before_widget' => '<div class="'.$sidebar_class.'"><div id="%1$s" class="widget %2$s" >',
'after_widget' => '</div></div>'
)
);
}
add_action('widgets_init','sidebar_widget_init');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0