Found this handy little function the_widget which you may find useful at some stage.
There’s at least 2 ways to use the function which is normally the case, (1)either add the template directly to your template file or (2)include it in a custom function from your functions.php file.
Here’s the default usage for direct use in template files which includes 3 parameters.
<?php the_widget( $widget, $instance, $args ); ?>
The first parameter, $widget, should be replaced with the PHP class name of the widget.
You can grab any of the native WordPress widget classes directly from the default-widgets.php file in the WordPress core files.
<?php the_widget( 'WP_Widget_Archives', $instance, $args ); ?>
Here’s the most basic usage of the template tag:
<?php the_widget( 'WP_Widget_Archives' ); ?>
You can also use the_widget function in custom functions with hooks and conditional tags.
Here’s an example of the most basic usage in a custom function:
And here’s an example of using the same widget class with all parameters for the function the widget uses which in this case is the wp_get_archives function.
Was This Tutorial Helpful?