而您可以查看WidgetControl
包含在SidebarSection
, 相反,最好是查看底层setting
它列出了侧栏中包含的小部件。所以请这样做:
wp.customize( \'sidebars_widgets[sidebar-1]\', function( sidebarSetting ) {
console.info( sidebarSetting.get().length );
} );
或者,使用可重用功能:
/**
* 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();
}
用法:
getSidebarWidgetCount( \'sidebar-1\' ).done( function( count ){
console.info( count );
} );