Displaying WordPress Widget Directly from Template File

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' );

Inspire us with your love!

FacebookTwitterReddit
Editorial Staff

aThemeArt editorial staff provides and makes all the content that is published on athemeart.com. In addition, they are responsible for all web content types, including blogs, social posts, videos, documentation, etc.

You can check also