Advertisement
  1. Code
  2. WordPress
Code

Dynamically Adding Four Footer Widget Areas

by
Difficulty:IntermediateLength:LongLanguages:

These days, many WordPress themes have a number of widget areas in the footer, meaning you can create a "fat footer" with multiple widget areas side by side. It's something I use on all of my themes.

But what if your theme has four widget areas but you only want three widgets? If you're not careful, you'll end up with an ugly gap on the right-hand side where the empty fourth widget area is.

In this tutorial I'll show you how to avoid this using a combination of conditional tags in PHP and some Object-Oriented CSS (OOCSS), which will check how many widget areas you've populated and automatically resize them so they each take up the right proportion of the page's width.

What You'll Need

To follow this tutorial, you'll need:

  • a development installation of WordPress
  • your own theme to which you'll add the code
  • a code editor

Registering the Widget Areas

The first step is to register the four widget areas for your footer. If you don't already have any widget areas registered, you'll need to add this code to your functions.php file:

This will add four footer widget areas to your theme. If you view the Widgets screen you'll see them all there, along with any others you've already registered:

Adding the Widget Areas to Your Theme

If you add widgets to those widget areas now, nothing will happen. You need to add them to your theme's footer. I'll work through the code you need to add to your footer file in stages, adding more checks for different numbers of populated widget areas as we go along.

Note that you may need to alter some of the classes and elements containing the widget area code in line with your theme's structure and layout.

1. Checking if No Widget Areas Are Populated

The first step is to check if no widget areas are populated, in which case nothing should be output. Open your theme's footer.php file and add the code below.

2. Checking if All Widget Areas Are Populated

Next, I'll check if all of the widget areas are populated. If this is the case, the theme will output the contents of all four, with each of them floated next to each other and taking up a quarter of the width of the screen. I'll add the classes for this here, but add the CSS to my stylesheet later.

After the line reading return; but before the endif; statement in the code you just added, add this:

This does the following:

  • Checks that all footer widget areas are populated using if tags.
  • If so, opens a new element called <aside class = "fatfooter">. This extra element containing the widget areas allows the whole footer to be centred on the page even if the footer itself isn't (for example if the whole footer has a wide background).
  • Outputs all four widget areas using the dynamic_sidebar() function, giving each of these classes for layout.

The classes are .left.quarter for most of the widget areas, except for the last one which has .right.quarter.

3. Checking if Just Three Widget Areas Are Populated

The next step is to add the code to output the widget areas if just three of them are populated. Below the code you just added, insert the following:

This checks if the first three widget areas are populated and the fourth isn't, and then outputs the first three using appropriate classes—replacing .quarter with .one-third.

Note that when you're populating your widget areas, you always need to miss out the last one(s) if you don't want to use them all, or this check won't work. Don't leave the first one empty and populate the later ones.

4. Checking if Just Two Widget Areas Are Populated

To output just two widget areas if they've been populated and the third and fourth ones haven't, add this code below the code you just added:

This works in a similar way to the previous two code snippets, using the .half class for layout.

5. Checking if Just One Widget Area is Populated

The final check is for just the first widget area being populated. Add this below the code you just added:

This will output the one widget area's contents in a full-width element.

Now save your footer.php file.

Adding the CSS

Right now your widget areas won't work correctly—in fact, if you populate them all they'll appear one below the other at full width. So you need to add some OOCSS to make those layout classes work. The OOCSS I'm adding is responsive, so I've included media queries to resize the footer widget areas on small screens.

1. Adding Floats

First let's make sure the widget areas float alongside each other.

Open your theme's style.css file and add this code:

2. Adding Widths

The widths will ensure that all of the widget areas take up the correct proportion of the screen. I'm using percentages as it makes the code more flexible and responsive. Add this to your stylesheet:

Note that the widths don't add up to 100% because space needs to be allowed for margins.

3. Adding Margins

Now add the styling for margins to your stylesheet:

Note that the margins are different for elements which also have the .right or .left classes applied to them, so that they sit flush at the edge of their containing element.

4. Adding Media Queries

The media queries will do two things:

  • On small screens such as tablets, four elements will appear in a two by two grid, meaning that elements with the .quarter class will have half width and margins will be adjusted accordingly.
  • On very small screens such as smartphones, all elements will be full width.

Add the media queries at the bottom of your stylesheet along with any other media queries you may have:

Now save your stylesheet.

Testing Your Code

Now it's time to see what happens when you populate your widget areas.

Below is a screenshot of a site I manage which makes use of this code. Normally the footer has four widget areas:

Footer with four widget areas

If I remove the widget from the fourth widget area, just three are displayed, with equal widths:

Footer with three widget areas

And if I remove widgets from the third widget area, visitors will see two widget areas each taking up half of the width:

Footer with two widget areas

Summary

This technique is useful if you want your theme to allow for the population of multiple footer widget areas but you don't know how many of those widget areas will actually be used.

If you're creating a framework, a starter theme, or a theme which other people will use for their sites, it can help make the footer layout tidier and save extra work down the line adding layout CSS.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.