How to Add a Different Sidebar for Different Pages in GeneratePress - WP Developers

How to Add a Different Sidebar for Different Pages in GeneratePress

I’ve decided to give GeneratePress a shot to rebuild this site – a number of people I know use it – and while I find it a quite limiting and opinionated, it has helped me get a site up and running within a day, so I can’t argue with that.

One of the bugbears I had was the fact I couldn’t use a different sidebar for different contexts.

For example, if you run a WooCommerce store, you would probably want to display a cart, some filters and any other call to actions in the sidebar. If that same store had a blog, you might want to display different widgets, such as a mail opt-in box.

Normally with Sage, my theme of choice (and will continue to be for any client work), you can simply create a file called sidebar-{post-type}.php in the root of the theme, and it will display it whenever you access a page within that post type. Totally no-nonsense.

With GeneratePress, however, it’s not quite as easy as that. Having dug around in the support forums to see if there was something I’m missing, it seems as if the recommended approach is to install a plugin, such as Content-aware Widgets and specifying on each widget where it should appear. Urgh.

Instead, I decided to take a look and see if there was any way of doing this in a sightly cleaner way. To GeneratePress’ credit, it is very hookable, so this was possible without too much fuss.

There are 3 steps to make this happen:

  1. Register a new sidebar area within WordPress
  2. Create the template file for the sidebar area
  3. Replace the default GeneratePress sidebar with our own

Registering a new WordPress Sidebar

This step is very simple. If you’re following along, this code can go in your functions.php file, or in a plugin.

Bear in mind, in my case I’m trying to display a specific widget area/sidebar on my course pages, but you can swap the labels for whatever you like.

Now, in Appearance > Widgets, you should see the new widget area:

GeneratePress custom sidebar per post type

Creating the Template File

Next up, we’ll create the template file. This needs to go in the root of your theme (or child theme).

Give this a name prefixed by sidebar-. In my case, I have called the file sidebar-course.php This doesn’t need to correlate to the post type name.

You’ll notice that we are using the function dynamic_sidebar() to pull in the ‘course’ sidebar. This matches the ID specified in the first step within register_sidebar().

Replace the GeneratePress Sidebar with our own

The first thing to mention here is that I am inside a child theme, and so I’ve had to do this slightly differently. We need to remove the function associated with WordPress action in the parent theme, from the child.

To do this, we need to hook twice. First we need to hook into the wp action, and within that hook, we can then remove and add the functions we need.

And that’s it!

Here's the thing... I love to write and teach, but I'm terrible at self-promotion. If you found this article interesting, I'd really appreciate if you'd share this article with people you think would also enjoy it.

Leave a Comment