我正在使用original post by birgire, 但我不知道使用wp\\u get\\u sidebars\\u widgets()是否“可以”;以这种方式。
function wpse_show_widget_alt( $index, $id ) {
global $wp_registered_widgets, $wp_registered_sidebars;
$did_one = FALSE;
//This built-in func isn\'t meant for theme or plugin development.
//I don\'t know any other way to list widgets in an array sorted by sidebar.
$theWidgets = wp_get_sidebars_widgets();
$widgetNumber = $theWidgets[$index][$id];
// Check if $id is a registered widget
if( ! isset( $wp_registered_widgets[$widgetNumber] )
|| ! isset( $wp_registered_widgets[$widgetNumber][\'params\'][0] ) )
{
return FALSE;
}
// Check if $index is a registered sidebar
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty( $wp_registered_sidebars[ $index ] )
|| empty( $sidebars_widgets[ $index ] )
|| ! is_array( $sidebars_widgets[ $index ] ) )
{
return FALSE;
}
// Construct $params
$sidebar = $wp_registered_sidebars[$index];
$params = array_merge(
//array( array_merge( $sidebar, array(\'widget_id\' => $id, \'widget_name\' => $wp_registered_widgets[$id][\'name\']) ) ),
array( array_merge( $sidebar, array(\'widget_id\' => $widgetNumber, \'widget_name\' => $wp_registered_widgets[$widgetNumber][\'name\']) ) ),
(array) $wp_registered_widgets[$widgetNumber][\'params\']
);
// Substitute HTML id and class attributes into before_widget
$classname_ = \'\';
foreach ( (array) $wp_registered_widgets[$widgetNumber][\'classname\'] as $cn )
{
if ( is_string($cn) )
$classname_ .= \'_\' . $cn;
elseif ( is_object($cn) )
$classname_ .= \'_\' . get_class($cn);
}
$classname_ = ltrim($classname_, \'_\');
$params[0][\'before_widget\'] = sprintf($params[0][\'before_widget\'], $widgetNumber, $classname_);
$params = apply_filters( \'dynamic_sidebar_params\', $params );
// Run the callback
$callback = $wp_registered_widgets[$widgetNumber][\'callback\'];
if ( is_callable( $callback ) )
{
call_user_func_array( $callback, $params );
$did_one = TRUE;
}
return $did_one;}