Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

the_widget();

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:

the_widget('openingHours');

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

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

share|improve this question

1 Answer 1

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 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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