While you can look at the WidgetControl
instances contained inside of a SidebarSection
, it is better to instead to look at the underlying setting
that lists out the widgets contained inside of the sidebar. So do something like this:
wp.customize( 'sidebars_widgets[sidebar-1]', function( sidebarSetting ) {
console.info( sidebarSetting.get().length );
} );
Alternatively, with a reusable function:
/**
* Get the count of widgets in a given sidebar.
*
* @param {string} sidebarId Sidebar ID.
* @returns {jQuery.promise} Resolving with the number of widgets in the sidebar.
*/
function getSidebarWidgetCount( sidebarId ) {
var deferred = jQuery.Deferred();
wp.customize( 'sidebars_widgets[' + sidebarId + ']', function( sidebarSetting ) {
deferred.resolve( sidebarSetting.get().length );
} );
return deferred.promise();
}
Usage:
getSidebarWidgetCount( 'sidebar-1' ).done( function( count ){
console.info( count );
} );