Im trying to print my own created widget with the following function:


The widget is defined with the following values:

$this->WP_Widget('openingHours', 'Opening', $widget_ops, $control_ops);

When I try to get the widget I use the following code:


Although nothin shows up. I have added info to the widget at the admin panel. And this code also prints the widget the right way.

if ( !function_exists( 'Footer 2' ) || !dynamic_sidebar() ) : 
                        dynamic_sidebar( 'Footer 2' );

What am I doing wrong with "the_widget()" function and how should I do it right?


For reference, the_widget

The way the_widget() works, is that you must either pass it $instance (array/string) which will have all of the form values of the widget. They give an example of the Archives Widget on the codex page. You can also pass $args which controls what text is displayed before/after the widget and the widget's title. You can probably leave out $args, as the default probably will keep any styling consistent through the site.

So, you need to build an $instance array containing all the needed parameters for your widget to function. The keys of the array will be your form field names, and the values will be.. well, the values!

Codex example:

$instance = array(
    'title' => 'Post Archives',
    'count' => 6
    'dopdown' => 0 // default
the_widget( 'WP_Widget_Archives', $instance );

Your Widget:

$instance = array(
    'your_args' => 'their values'
the_widget( 'openingHours', $instance );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.