描述:

在侧边栏外显示任意小工具。它可以在模板中的任何位置使用。

用法:

<?php the_widget( $widget, $instance, $args ); ?>

参数:

$widget (string) 必填 小工具的php类名 WordPress中包含的小工具的类是:

  • WP_Widget_Archives -- Archives
  • WP_Widget_Calendar -- Calendar
  • WP_Widget_Categories -- Categories
  • WP_Widget_Links -- Links
  • WP_Widget_Meta -- Meta
  • WP_Widget_Pages -- Pages
  • WP_Widget_Recent_Comments -- Recent Comments
  • WP_Widget_Recent_Posts -- Recent Posts
  • WP_Widget_RSS -- RSS
  • WP_Widget_Search -- Search (a search from)
  • WP_Widget_Tag_Cloud -- Tag Cloud
  • WP_Widget_Text -- Text
  • WP_Nav_Menu_Widget 默认值: None

$instance(array|string) (可选)  小工具的实例设置。数组或字符串。请参阅下面的每个小工具以获取示例。

默认值: array()

$args(array|string) (可选) 小工具的查询参数。数组或字符串。如果为空,则默认为: before_widget (string)

在输出的小工具前面添加的信息 默认: <div class="widget {widget's classname}">

after_widget (string)

在输出的小工具之后添加的信息 默认: </div>

before_title (string)

小工具标题之前的信息.

默认: <h2 class="widgettitle"> after_title (string) 小工具标题之后的信息. 默认: </h2>

默认值: array()  

源文件:

function the_widget( $widget, $instance = array(), $args = array() ) {
    global $wp_widget_factory;
if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
/* translators: %s: register_widget() */
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Widgets need to be registered using %s, before they can be displayed.' ), 'register_widget()' ), '4.9.0' );
 return;
} 
$widget_obj = $wp_widget_factory->widgets[ $widget ]; 
if ( ! ( $widget_obj instanceof WP_Widget ) ) { return; } 
$default_args = array(
'before_widget' => '',
'after_widget'  => '',
'before_title'  => '',
'after_title'   => '',
);
$args = wp_parse_args( $args, $default_args ); 
$args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); 
$instance = wp_parse_args( $instance ); 
/** 
* Fires before rendering the requested widget. 
* 
* @since 3.0.0 
* 
* @param string $widget The widget's class name. 
* @param array $instance The current widget instance's settings. 
* @param array $args An array of the widget's sidebar arguments. 
*/ 
do_action( 'the_widget', $widget, $instance, $args ); 
$widget_obj->_set( -1 );
$widget_obj->widget( $args, $instance ); }

参与评论