You probably already know that we can add widgets to any widget area from Widgets setting page in WordPress Dashboard.
But did you know that you can display any widget on any template file? So for example you want to display a widget in the header but there’s no widget area in the header. What would you do?
the_widget function comes to the rescue!
You can use the code below to load any widget on any template file.
the_widget( $widget, $instance, $args );
$widget= widget’s PHP class name
$instance= widget’s instance settings
$args= widget’s sidebar args, for example before_widget, before_title and so on
To know all of the above parameter, you will need to open the widget file.
So, let say you want to display the Search widget in the header. Open up header.php file then put this code in the place where you want the widget to appear:
the_widget( 'WP_Widget_Search' );
Another example, use this code to display Category widget and display it as dropdown:
the_widget( 'WP_Widget_Categories', 'dropdown=1' );
912 total views, 1 views today