Quantcast
Viewing latest article 2
Browse Latest Browse All 2

Using the_widget to Display Widgets within your WordPress Template files

Code Snippets for WordPress are highly useful blocks of code that we can add to our WordPress sites to extend it’s default functionality. Whether you are an experienced WordPress Developer or a weekend Blogger useful code snippets are sure to make your life easier.

Displaying WordPress Widget Directly from Template File

Did you know that aside from adding the standard WordPress widgets from within the WordPress Dashboard, that there is a built in function available called the_widget() that allow you to place a widget anywhere within your theme template files.

 

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' );
Be sure to check out our other great WordPress Code Snippets and WordPress Articles.

Viewing latest article 2
Browse Latest Browse All 2

Trending Articles