• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2785

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

Display Any Native WordPress Widget Anywhere in Your Theme

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.

Register for full access

Reader Interactions

Comments

  1. Lynne says

    August 12, 2014 at 5:26 pm

    Great post as usual but I have a question.

    I have been using custom menus in sidebar widgets, each page having its own sidebar menu. However, with mobile, the menu goes down below the content. If I am understanding correctly I could use WP_Nav_Menu_Widget to display a menu in the content area in the first column so it stays on top?

    If so how do I choose which menu I want to display on the different pages?

    Log in to Reply
    • Brad Dalton says

      August 12, 2014 at 5:54 pm

      Thanks Lynne.

      Conditional tags or custom fields if you want alot of different menus.

      I think you can also rig the Genesis Simple Sidebars plugin to display in different hook positions.

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags