With many of the newest child themes for the Genesis Framework released on StudioPress, there is a home page that is set up to be run by widgets inside of sidebar areas that are specific to the homepage only. This makes it easy for “non-technical” people using those themes to determine exactly what gets displayed where on their home page. You can actually see this in the Scribble theme that I’m using for this site, where the home page is actually a series of Widgets. There’s a way to not only do the same thing on a Category Archive page, but also to make it dynamic so that the end users can create new categories and automatically have new widgets.
Why would you need to do this?
The reason I came up with this was actually for a series of client sites that I was working on, the Town Does sites. They all use the same custom theme that I developed, so I needed a way for one theme to be flexible for each town site. This was needed because they wanted a category archive page for each site that could be set up to look like a “home” page for that town. If the words in the past few sentences don’t make sense, you can see what I mean by visiting DowningtownDoes and clicking on any of the links under the “Towns” drop-down menu.
This is certainly a unique case, but I wanted to share my solution because I believe that others may want to do something similar for their own purposes. And I have to admit, I felt pretty proud of my creativity on this one. 🙂
Logical flow of operations
Here’s the steps that the code needs to go through to generate what is needed:
- Get list of sub-categories of “towns” (register-sidebars.php)
- Register widget areas for each sub-category (register-sidebars.php)
- Determine if we are displaying one of the town categories
- Output specific styling for the town category we are displaying
- If Widgets are active, then un-register default loop and replace with widget areas
This is set up to look for category named “Towns”, and look for any categories that are children of that category. This is where there is flexibility across each of the sites. Of course, if you’re taking this and using it for your own purposes, you can make the parent category be anything that you want, but the key is that you have a parent category with child categories.
Let’s get into the code that actually runs this thing. First, there are a few functions that need set up to make our job easier in later steps. This first function is used to get an array of categories that have the parent category named “Towns”.
I’ve set this up so that I can re-use it again later with a different parent category if needed, but the default usage has all the settings that I want for now. If there are any child categories of the “Towns” category, it finds them using the get_categories() function. If no categories are found, then an empty array is returned.
This next function is used to output the city/town-specific styling. It has a filter so that the town name can override the city name that is set by the theme settings.
Here’s the function that determines the hex color code for each city. So far, adding entires to this list has been the only manual code updating I’ve had to do, but it is minimal, so I didn’t bother setting up another solution.
Finally, this little function is what will actually do the filtering of the town name, when called.
Now, we can get on with putting it all together.
1. Get list of sub-categories of “towns” (register-sidebars.php)
This is pretty simple. I use the function I created earlier to get an array of the town categories and store it in a variable.
2. Register widget areas for each sub-category (register-sidebars.php)
I created a file named “register-sidebars.php” inside of a “tools” subfolder of my theme. As you can probably guess by the name, this file contains all the code to register sidebar areas. I need 3 sidebar areas per category, to create a Featured Top section, and 2 columns below that. I admit, I could probably use a Genesis Grid Loop in place of the 2 lower sidebars, but this was working, so I went with what I had:
To break this piece down, first I loop through the array from step 1, creating 3 widget areas for each sub-category. Within that loop, I first check to see if each category has a sub-category that is used for “featured” posts. If it exists, then I display the ID of that category. If it doesn’t, then I let the user know that the category doesn’t exist, and tell them where to go to create it.
Why do I need the ID of a “featured” sub-category? Simple: I use the Genesis Featured Widget Amplified plugin, which allows me to exclude posts by one or more category IDs. Having the ID readily available makes it very easy for someone dropping in instances of that widget to know which ID they need to put in there to exclude.
3. Determine if we are displaying one of the town categories
4. Output specific styling for the town category we are displaying
5. If Widgets are active, then un-register default loop and replace with widget areas
The remainder of the steps take place within my category.php file. This is the file that WordPress automatically looks for whenever you’re displaying a category archive. We use this to our advantage to save ourselves a lot of code writing.
Here’s the first piece:
First I use get_queried_object() to get the WordPress query object. Then I can check the object to see if the category requested is one of the “Towns” sub-categories. Once I know that it is, I add the style filter, then I check to see if any of the sidebar areas actually have widgets in them. By doing this, I’m able to keep the default output if the widgets haven’t been set up yet. This is the same type of check that is used on most of the newer Genesis child themes.
Once I know that the sidebar areas have active widgets, I remove the main Genesis loop, and replace it with the next 2 functions that display the widget areas:
The only thing remaining is to run the genesis() function at the end of the file.
The only thing that you have to do that I didn’t specifically mention is include any extra files (with the exception of category.php) in your functions.php file. I usually just use require_once to do so.
I’d love feedback on not only this process, but this tutorial. It uses a very specific example, but I felt that the method I used was still something that would be worthwhile to share with others. Let me know in the comments below!