the_widget() - WordPress 函数参考中文文档

返回介绍

the_widget()

发布于 2017-09-11 字数3828 浏览 748 评论 0

the_widget( string $widget,  array $instance = array(),  array $args = array() )

Output an arbitrary widget as a template tag.


描述


参数

$widget

(string)
(Required)
The widget’s PHP class name (see class-wp-widget.php).

$instance

(array)
(Optional)
The widget’s instance settings.

Default value: array()

$args

(array)
(Optional)
Array of arguments to configure the display of the widget.

  • ‘before_widget’
    (string) HTML content that will be prepended to the widget’s HTML output. Default <div class="widget %s">, where %s is the widget’s class name.
  • ‘after_widget’
    (string) HTML content that will be appended to the widget’s HTML output. Default </div>.
  • ‘before_title’
    (string) HTML content that will be prepended to the widget’s title when displayed. Default <h2 class="widgettitle">.
  • ‘after_title’
    (string) HTML content that will be appended to the widget’s title when displayed. Default </h2>.

Default value: array()


源代码

File: wp-includes/widgets.php

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;
	}

	$default_args = array(
		'before_widget' => '<div class="widget %s">',
		'after_widget'  => "</div>",
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => '</h2>',
	);
	$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);
}

更新日志

Version 描述
2.8.0 Introduced.

相关函数

Uses

  • wp-includes/functions.php:
    wp_parse_args()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/widgets.php:
    the_widget

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Nico Andrade

    Example:

    
    $args = array(
        'before_widget' => '<div class="widget %s">', 
        'after_widget' => '</div>',
        'before_title' => '<h2 class="widget-title">',
        'after_title' => '</h2>'
        );
    $instance = array(
        'title' => 'Title',
        'text' => 'Text'
        );
    the_widget( 'My_Custom_Widget', $instance, $args );
    

上一篇:the_weekday_date()

下一篇:timer_start()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!