How can I add a specific, custom widget to my theme’s header.php?

I’ve created a custom widget that I’ve named openingTimesWidget which extends the WP_Widgets class and follows the guidelines for creating widgets…

What I’d now like to do is not only add this to a sidebar but be able to call upon it individually…So far I’ve tried using the_widget('openingTimesWidget') function directly within some HTML but it returns nothing…

1 Answer
1

The WordPress codex mentions you should call the Widget Class name. Could it be that openingTimesWidget is a function instead of a Class? Following the WP Class naming convention your class would be written like this Opening_Times_Widget.
https://codex.wordpress.org/Widgets_API

Another possible reason that the widget is not displaying could have to do with the way your theme loads. I encountered a situation with the Roots theme where a widget in the site header would not load. This was caused by the specific way that Roots loads the theme.

Leave a Comment