Creating a Custom Taxonomy for Departments on a Company Site

Creating a Custom Taxonomy for Departments on a Company Site

I often get asked if WordPress Multisite is a suitable solution for a company website that has different divisions with unique identities. The answer is usually yes – Multisite can do this excellently.

But it isn’t always necessary to create a separate site for each of the divisions in your company website, even using Multisite. If you want to include shared content that’s not specific to each division, or your divisions’ identity has a lot in common, or you’re working with an existing single site installation, then there is a way to get around this without activating Multisite.

In this post, I’m going to show you how to do it using a custom taxonomy. We’ll create a “division” taxonomy, which is applied to posts and pages, so you can specify which sections of your website each post or page should be in. And then we’ll create a template file for your taxonomy, so that you can display content for each division in just the way you want to.

You will need to write some code, creating a plugin and a file in your theme, but the code isn’t difficult. I’m going to use a child theme of the twenty sixteen default theme but you can use this technique with your own theme. I’ll also create a plugin to register the custom taxonomy – it’s better practice to do this with a plugin so that you don’t lose your taxonomy if you change themes in the future.

So let’s get started!

Getting Started

Before you start, you’ll need:

  • A development or test installation of WordPress, which is a copy of your existing company website if you have one. Don’t do this on your live site – you can always upload your files to live once it’s all working.
  • A code editor.
  • A theme – which can be your own theme or a child of twenty sixteen like I’m using. If you’re using a child theme you’ll also need to ensure that twenty sixteen is installed.

Start by creating your child theme and activating it. I’m not going to go into detail on how you do this as you can read our post on child themes, but here’s the code for your child theme’s stylesheet:

You may notice from that code that I’ve uploaded the files for this post to GitHub, where you can download it. I’ve also created a demo site.

Now activate your child theme in your site. You’ll find that it looks exactly the same as if you had twenty sixteen activated:

the starting home page

Registering the Taxonomy

The next step is to register our taxonomy by creating a plugin. You may be tempted to add the code to the functions file in your theme, but the problem with that is that if you change your theme in the future, you’ll lose your taxonomy – and access to any terms you’ve created for it in the WordPress admin.

Create a new file in your plugins folder (which is in wp-content) and give it a suitable name. Open it with the following comments to tell WordPress what it is:

Now add your function to register the taxonomy:

This registers a taxonomy called division and creates the relevant labels for it, including its name, menu name and more. It also specifies that it applies to both posts and pages, with the array( 'post', 'page' ) argument in the register_taxonomy() function.

Now activate your plugin in your site. You’ll see that you have access to the new taxonomy when you’re editing posts and pages:

The post editing page includes our new taxonomy

You can now create taxonomy terms for each of the divisions in your company and then assign them to your posts and pages.

Displaying Your Divisions in the Site

I’ve now created some dummy content and assigned divisions to my posts and pages. Here are some of my posts:

The post listing page in the admin - with divisions applied

I’ve also created some pages for my divisions, as well as some more generic pages:

The page listing page in the admin - with divisions applied

You can now add all of your pages and divisions to the website’s main navigation menu. Don’t add your posts, as they’ll be displayed in archive pages. I’ve done this in my Menus admin screen:

The navigation menu with pages and taxonomy term archives added

WordPress will automatically create an archive page for each of your divisions and display these using the best template file in your theme, using the theme template hierarchy.

So here’s my archive page for the Research and Development taxonomy:

Default taxonomy archive for R&D, using twenty sixteen's template file

WordPress has done its job well here. It’s included not only posts but pages too, as the taxonomy is applied to them. However it’s displaying them in date order, as you’d expect. I’d like to display all of the pages first followed by all the posts. To do that I’ll need to create a custom taxonomy archive with a custom version of the loop.

Creating a Taxonomy Archive

To display our taxonomy archive pages correctly, we need to create a theme template file called taxonomy-division.php. Let’s do that by copying an existing template file and editing it.

If your theme has an archive.php file make a copy of that and call it taxonomy-division.php. If you don’t have an archive template file, make a copy of index.php instead. Because I’m working with the twenty sixteen theme I’m going to make a copy of the archive.php file from twenty sixteen in my child theme and call it taxonomy-division.php.

First edit the comments at the beginning of the file to more accurately show what this file is for:

Now find the code for the loop. In my file copied from twenty sixteen it looks like this:

This calls in a template part from twenty sixteen called content.php. We’re going to keep that but we’ll run it twice: once for posts and once for pages.

Edit the loop in your template file so it reads like this:

That runs the loop twice but only queries the database once, which makes it more efficient than using WP_Query to create a completely new query. It starts off by checking if the current post is of the ‘page’ post type and displaying it if that’s the case, then it rewinds the posts and does the same for the ‘post’ post type.

Save this and you’ll find that your archive page displays pages first:

The taxonomy archive displaying pages before posts

And here’s the full taxonomy template file:

Styling Divisions Differently

So we now have a template file that displays pages and posts for our divisions the way we want it to. Now let’s make those divisions look different from each other by adding some styling.

You can choose to style your divisions any way you want, and you’ll probably want to do more than I’m doing here, but for now I’ll show you how to change the colours for some parts of the pages, posts and archive pages for each division, to make them look a bit different.

You do this in your child theme’s stylesheet (or in your theme stylesheet if you’re not using a child theme).

Open your stylesheet and add this styling:

This adds styling for the heading in archive pages by targeting the classes applied to the body element, based on the taxonomy terms I’ve used. You’ll need to edit yours with your own taxonomy terms – you can find the classes generated by inspecting the HTML output in a taxonomy archive page in your site and examining the body element.

Now my R&D taxonomy archive has some yellow added to differentiate it:

taxonomy archive with yellow styling

You could add more to this, maybe styling posts in your main blog page from different divisions and styling other elements on the page in the archive pages or in single posts. You could also style the links on your main navigation menu to match. But you can see here how it’s possible to create bespoke styling for each division in your company website.

WordPress Makes it Easy to Create a Company Site with Separate Divisions

Using the techniques I’ve shown you here, you can create a company website with separate sections for each division, that displays content relating to that division. You can also use the CSS classes that WordPress adds to the body element to style those divisions differently.

You could take this much further, adding images, backgrounds or even changing fonts via your stylesheet, or creating a separate template file for each of your taxonomy terms, using the template hierarchy to name them correctly. All of which means that if you’re working with an existing site or don’t want to separate your company divisions completely, a single WordPress site can still give each of your divisions its own identify.

Got any questions about the code we've covered in this tutorial? Ask away in the comments below.

Rachel McCollin

Rachel McCollin Rachel is a freelance web designer and writer specializing in mobile and responsive WordPress development. She's the author of four WordPress books including WordPress Pushing the Limits, published by Wiley.