function get_widget_ids_in_sidebar($sidebar_id) { $widget_ids = array(); //Get the list of gadgets in the specified sidebar $widgets = wp_get_sidebars_widgets(); if (isset($widgets[$sidebar_id])) { $widget_ids = $widgets[$sidebar_id]; } return $widget_ids; } //Output widget numbers in the sidebar $sidebar_id = 'sidebar'; // ID of the sidebar $widget_ids = get_widget_ids_in_sidebar($sidebar_id); //Traverse the widget list and output the widget number foreach ($widget_ids as $widget_id) { Echo "Widget No.: $widget_id<br>"; }