您的sidebar-1
可能是
[sidebar-1] => Array
(
[0] => categories-2
[1] => archives-4
[2] => recent-comments-4
[3] => calendar-2
[4] => search-2
[5] => archives-2
[6] => text-4
[7] => recent-posts-2
[8] => nav_menu-2
)
例如,如果您得到
calendar-2
widget,然后来自:
print_r( $GLOBALS[\'wp_registered_widgets\'][\'calendar-2\'] );
您将得到如下结果:
[calendar-2] => Array
(
[name] => Dagatal
[id] => calendar-2
[callback] => Array
(
[0] => WP_Widget_Calendar Object
(
[id_base] => calendar
[name] => Calendar
[widget_options] => Array
(
[classname] => widget_calendar
[description] => A calendar of your site’s Posts.
)
[control_options] => Array
(
[id_base] => calendar
)
[number] => 2
[id] => calendar-2
[updated] =>
[option_name] => widget_calendar
)
[1] => display_callback
)
[params] => Array
(
[0] => Array
(
[number] => 2
)
)
[classname] => widget_calendar
[description] => A calendar of your site’s Posts.
)
因此,您可以尝试此修改版本的
dynamic_sidebar()
要调用放置在给定侧栏中的小部件,请执行以下操作:
/**
* Show a given widget based on it\'s id and it\'s sidebar index
*
* Example: wpse_show_widget( \'sidebar-1\', \'calendar-2\' )
*
* @param string $index. Index of the sidebar where the widget is placed in.
* @param string $id. Id of the widget.
* @return boolean. TRUE if the widget was found and called, else FALSE.
*/
function wpse_show_widget( $index, $id )
{
global $wp_registered_widgets, $wp_registered_sidebars;
$did_one = FALSE;
// Check if $id is a registered widget
if( ! isset( $wp_registered_widgets[$id] )
|| ! isset( $wp_registered_widgets[$id][\'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) $wp_registered_widgets[$id][\'params\']
);
// Substitute HTML id and class attributes into before_widget
$classname_ = \'\';
foreach ( (array) $wp_registered_widgets[$id][\'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\'], $id, $classname_);
$params = apply_filters( \'dynamic_sidebar_params\', $params );
// Run the callback
$callback = $wp_registered_widgets[$id][\'callback\'];
if ( is_callable( $callback ) )
{
call_user_func_array( $callback, $params );
$did_one = TRUE;
}
return $did_one;
}
你这样称呼它:
wpse_show_widget( \'sidebar-1\', \'calendar-2\' );
另一种方法是尝试使用
the_widget()
函数,您必须知道小部件的类名。