I fetched widget id this way:
$the_sidebars = wp_get_sidebars_widgets();
print_r( $the_sidebars['sidebar-1'] );
It shows:
Array ( [0] => recent-posts-2 [1] => recent-comments-2
[2] => archives-2 [3] => categories-2 [4] => meta-2 )
Now I just want to show the recent-posts-2
widget, which means I just want to pass the widget ID recent-posts-2
to the function, and the function will display the widget, such as the following code:
function display_widget($widget_id) {
...
return $wid;
}
When I echo display_widget($widget_id)
, it will show the widget with HTML, and the class of the default theme.
Do you have any idea?
You can use
wp_get_sidebars_widgets()
with an “on demand” filter callback. This means we’re adding the filter callback, right before the call to the function and then remove right inside the callback again. This allows us, to use it only once. It also means, that we need to set it right before the call towp_get_sidebars_widgets()
every time we need only the specified widget.If you need different widgets on different pages, just add a
switch/foreach
inside the callback and use conditional tags likeis_home()
, etc.