wordpress count widgets wordpress count widgets wordpress wordpress

wordpress count widgets


wp_get_sidebars_widgets()

Will give you an array of the sidebars and the widgets they have, just count the array for the appropriate sidebar..

For example:

$the_sidebars = wp_get_sidebars_widgets();echo count( $the_sidebars['my-sidebar-id'] );

The ID is the one you declare when you register the sidebar(so check your sidebar registration code).

You could also wrap this into a function.

function count_sidebar_widgets( $sidebar_id, $echo = true ) {    $the_sidebars = wp_get_sidebars_widgets();    if( !isset( $the_sidebars[$sidebar_id] ) )        return __( 'Invalid sidebar ID' );    if( $echo )        echo count( $the_sidebars[$sidebar_id] );    else        return count( $the_sidebars[$sidebar_id] );}

Then call it when you need to get the count, using the sidebar ID..

count_sidebar_widgets( 'some-sidebar-id' );

Or store in variable for other usage..

$my_var = count_sidebar_widgets( 'some-sidebar-id', false );

Hope that helps..


When you are building the sidebar, there should be a list containing the sidebar contents; you are iterating over it to add those items to the generated page. Just use the count() function on this list to find the number of items you're displaying.