the_widget()显示一个小工具/侧板栏

目录

描述

This template tag displays an arbitrary widget outside of a sidebar. It can be used anywhere in templates.

the_widget() 描述

用法

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

the_widget() 用法

参数

$widget

(string) (必填) The widget's PHP class name. The classes for the widgets included with WordPress are:

  • 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) (可选) The widget's instance settings. Either an array or query-style string. See each widget below for examples.

默认值: array()

$args

(array|string) (可选) The widget's sidebar args. Either an array or query-style string. If empty, it defaults to:

before_widget

(string) the text or HTML befor the widget.

Default: <div class="widget {widget's classname}">

after_widget

(string) the text or HTML after the widget.

Default: </div>

before_title

(string) the text or HTML before the widget's title.

Default: <h2 class="widgettitle">

after_title

(string) the text or HTML after the widget's title.

Default: </h2>

默认值: array()

the_widget() 参数

注意

  • 使用到: global $wp_widget_factory - To retrieve the widget object.
  • 使用到: do_action() - 调用 'the_widget' with $widget, $instance and $args.

the_widget() 注意

历史

the_widget() 历史

源文件

the_widget() 函数的代码位于 wp-includes/widgets.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Output an arbitrary widget as a template tag.
 *
 * @since 2.8.0
 *
 * @global WP_Widget_Factory $wp_widget_factory
 *
 * @param string $widget   The widget's PHP class name (see default-widgets.php).
 * @param array  $instance Optional. The widget's instance settings. Default empty array.
 * @param array  $args {
 *     Optional. Array of arguments to configure the display of the widget.
 *
 *     @type string $before_widget HTML content that will be prepended to the widget's HTML output.
 *                                 Default `
`, where `%s` is the widget's class name. * @type string $after_widget HTML content that will be appended to the widget's HTML output. * Default `
`. * @type string $before_title HTML content that will be prepended to the widget's title when displayed. * Default `

`. * @type string $after_title HTML content that will be appended to the widget's title when displayed. * Default `

`. * } */ function the_widget( $widget, $instance = array(), $args = array() ) { global $wp_widget_factory; $widget_obj = $wp_widget_factory->widgets[$widget]; if ( ! ( $widget_obj instanceof WP_Widget ) ) { return; } $before_widget = sprintf('
', $widget_obj->widget_options['classname'] ); $default_args = array( 'before_widget' => $before_widget, 'after_widget' => "
", 'before_title' => '

', 'after_title' => '

' ); $args = wp_parse_args($args, $default_args); $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); }

the_widget() 源文件

相关