Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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');
share|improve this answer

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');
share|improve this answer
    
Thanks. You can also add 'even' and 'odd' classes to the widgets. I did it that way originally, but I removed it because you can do the same with CSS3 selectors (nth-child). Anyway extra options don't hurt. –  nautilus7 Jun 7 '12 at 23:21
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] );
}
share|improve this answer
    
Thanks for the answer, but now I realize that what I want can't be done. At least with a method I know... I want to add a class on each widget based on how many of them are on a sidebar. For example if there are 4 widgets I want them to have class x or if they are 3 I want them to have class y etc. This can't be done with simply counting the number of widgets because the class (any class) they get is defined uppon sidebar registering, not drawing. At this point no widgets are set. –  nautilus7 Jun 4 '12 at 17:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.