Output an arbitrary widget as a template tag.
$widgetstringrequired
$instancearrayoptional
Default:array()
$argsarrayoptional
before_widget string<div class="widget %s">, where %s is the widget’s class name.after_widget string</div>.before_title string<h2 class="widgettitle">.after_title string</h2>.Default:array()
For parameter $widget, The classes for the widgets included with WordPress are:
Display a monthly archive list.
<?php the_widget( 'WP_Widget_Archives', $instance, $args ); ?>
widget_archive
__('Archives')
Default usage:
<?php the_widget( 'WP_Widget_Archives' ); ?>
Display drop-down list:
<?php the_widget( 'WP_Widget_Archives', 'dropdown=1' ); ?>
Displays a Calendar.
<?php the_widget( 'WP_Widget_Calendar', $instance, $args ); ?>
widget_calendar
Default usage:
<?php the_widget( 'WP_Widget_Calendar' ); ?>
Displays a list of categories.
<?php the_widget( 'WP_Widget_Categories', $instance, $args ); ?>
widget_categories
__( 'Categories' )
Default usage:
<?php the_widget('WP_Widget_Categories'); ?>
Display a dropdown list with number of posts.
<?php the_widget( 'WP_Widget_Categories', 'dropdown=1&count=1' ); ?>
Displays arbitrary HTML.
<?php the_widget( 'WP_Widget_Custom_HTML', $instance, $args ); ?>
widget_custom_html
Default usage:
<?php the_widget('WP_Widget_Custom_HTML', array('title' => 'Example', 'content' => '<p>Basic example.</p>')); ?>
Displays a list of links (blogroll) in categories.
<?php the_widget( 'WP_Widget_Links', $instance, $args ); ?>
alt attribute. The show_name parameter. Default: falseDefault usage:
<?php the_widget( 'WP_Widget_Links' ); ?>
Display lists in category IDs 2 or 3:
<?php the_widget( 'WP_Widget_Links', 'category=2,3' ); ?>
Display site meta. (Log in/out, admin, feed and WordPress links )
<?php the_widget( 'WP_Widget_Meta', $instance, $args ); ?>
widget_meta
__( 'Meta' )
Default usage:
<?php the_widget( 'WP_Widget_Meta' ); ?>
Display a list of Pages.
<?php the_widget( 'WP_Widget_Pages', $instance, $args ); ?>
widget_pages
__( 'Pages' )
menu_order
Default usage:
the_widget( 'WP_Widget_Pages' );
as the heading, sort by last modified date:
the_widget('WP_Widget_Pages', 'title=Contents&sortby=post_modified', 'before_title=<h3>&after_title=</h3>'); Display to a list of recent comments.
<?php the_widget( 'WP_Widget_Recent_Comments', $instance, $args ); ?>
widget_recent_comments
__( 'Recent Comments' )
Default usage:
<?php the_widget( 'WP_Widget_Recent_Comments' ); ?>
Display to a list of recent posts.
<?php the_widget( 'WP_Widget_Recent_Posts', $instance, $args ); ?>
widget_recent_entries
__('Recent Posts')
Default usage:
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
Display a list of entries from any RSS or Atom feed.
<?php the_widget( 'WP_Widget_RSS', $instance, $args ); ?>
Default usage:
<?php the_widget( 'WP_Widget_RSS' ); ?>
<?php the_widget( 'WP_Widget_Search', $instance, $args ); ?>
widget_search
Default usage:
<?php the_widget( 'WP_Widget_Search' ); ?>
<?php the_widget( 'WP_Widget_Tag_Cloud', $instance, $args ); ?>
__( 'Tags' )
post_tag
Default usage:
<?php the_widget( 'WP_Widget_Tag_Cloud' ); ?>
<?php the_widget( 'WP_Widget_Text', $instance, $args ); ?>
widget_text
Default usage:
<?php the_widget( 'WP_Widget_Text' ); ?>
Display custom widget in template.
<?php the_widget( 'My_Custom_Widget', $instance, $args ); ?>
Default usage:
class My_Custom_Widget extends WP_Widget
{
/* your code* /
}
<?php the_widget( 'My_Custom_Widget' ); ?>
function the_widget( $widget, $instance = array(), $args = array() ) {
global $wp_widget_factory;
if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: register_widget() */
__( 'Widgets need to be registered using %s, before they can be displayed.' ),
'<code>register_widget()</code>'
),
'4.9.0'
);
return;
}
$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 );
/** This filter is documented in wp-includes/class-wp-widget.php */
$instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args );
if ( false === $instance ) {
return;
}
/**
* 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 );
}
Fires before rendering the requested widget.
Filters the settings for a particular widget instance.
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/the_widget