0
votes

I created and registered widget. Everything looks cool with no errors. However, if i want to display widget by name for example in header (i am using custom widget area), it does not work. It creates all divs, but with no data included. If i am using code to display all widgets in that area - it works.

Question:

How to display widget by name?

Codes:

it displays all custom widgets in that area

<?php if ( is_active_sidebar( 'header-widget' ) ) : ?>
  <?php dynamic_sidebar( 'header-widget' ); ?>
<?php endif; ?>

Codex says, that this should work. However, it does not.

<?php the_widget( 'widget_simple' ); ?>

Thanks for any answers and sorry for bad english.

1

1 Answers

0
votes

If you are going to use the_widget, you need to supply the information that is normally supplied in the widget box on the WordPress Widget page.

So you would need:

<?php the_widget( 'widget_simple', $instanceData, $args ); ?>

$instanceData are any parameters you support in your widget, and $args are the parameters you specify when you define a sidebar, e.g. 'before_widget', 'after_widget', etc.

Both are optional, but if your widget requires some inputs (e.g. a post ID) and you don't specify $instanceData, then it's probably not going to do anything.

For example, if you wanted to display a standard WordPress text widget, the values would be:

<?php the_widget( 'WP_Widget_Text', array('text' => 'your text') ); ?>

since 'WP_Widget_Text" is the class of the standard text widget and 'text' is the parameter that specifies what test should be displayed.