Widgets

If you've ever set up a WordPress site, maybe using the default theme or one you downloaded from the theme repository, chances are that you've used widgets. Widgets are areas in a site or theme which can be set up via the WordPress admin, meaning site owners can add their own content without having to write a line of code.

The most common place to include widgets are in the sidebar and footer, but you could use them anywhere in your theme. For example, a widget area below the content gives you somewhere to display content listings, some share buttons or a call to action button beneath every single post.

WordPress comes with a number of widgets preinstalled and you can add more with plugins. Some of the preinstalled widgets are:

  • Archives – a list of posts by the date they were published.
  • Recent posts – list the latest posts from your blog.
  • Text – add some text or HTML (for example, you could use this to code a call to action button or add an image you've uploaded via the Media screen).
  • Categories – a list of the categories in your blog. If the user clicks one they'll see a list of all posts in that category.
  • Custom menu – a widget enabling you to place a custom menu in a widget area. For example, if you wanted to list your "small print" pages in the footer, you could create a menu listing them and then use this widget to display that menu in a footer widget area.
  • Tag cloud – a tag cloud showing the categories posts have been published to, with the most popular categories shown in larger text.

There are plenty more. To see them all, just take a look at the Widgets admin screen in WordPress.

But before we can add any widgets to our site we need to add widget areas to our theme, these will hold the individual widgets. This is similar to adding menus and has two stages:

  • In functions.php, register our sidebars, or widget areas
  • In the relevant template files or includes, add the code to display the appropriate widget area

So let's start by registering our sidebars in functions.php.

Registering sidebars or widget areas

For your theme to display any widget areas you must first register them in your theme's functions file, in much the same way as we did earlier for the menu. You can register as many as you need and then add the code to display them in the appropriate place in your theme.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset