ManagedCloud Servers

High performance handled and monitored by us 24/7/365. A complete solution to provide you with our in house expertise 24/7 tailored to your specific needs. We'll setup a bespoke server for your site using the latest tehnologies so you can get the most out of your hardware and get your website loading quickly and reliably. Find out more..

cPanelCloud Servers

Recommended - High performance cloud servers with no technical knowledge required. If you're hosting multiple websites already and you're looking to consolidate, or if you're looking to isolate yourself from the shared hosting environment but you don't have the time or knoweldge to manage a server, then the Managed cPanel Servers are for you. Find out more..

UnmanagedCloud Servers

Our unmanaged range gives you complete control at rock bottom prices and our cloud platform boasts super fast multipath 40Gb/s network, the latest Intel Xeon V3 CPUs and enterprise grade redundant SSDs. If you're a sysadmin look no further, we offer some of the best specification to price ratio servers available. Find out more..

Using Wordpress? Get our lightening fast Wordpress Optimised Hosting from £1.60 / month

View Plans

How to organize your WordPress site using Custom Taxonomies

WordPress is well-known as a blogging platform. Using some of the capabilities in-built in WordPress or using some plugins, it can be extended into a good Content Management System (CMS). The major difference between a blogging tool and a CMS is the ability to organize the content.

Quite often, when bloggers use WordPress for long, there is a need for better organization of their content. When followers increase, the ability to find posts by more than one criterion is required. The in-built categories and tags do not suffice. It is at this juncture that custom taxonomies come handy.

##Use case

For the purpose of this tutorial, let us assume that you possess a blog where you record your thoughts on your various interests -- politics, music, movies and books. You use categories to differentiate posts belonging to these various categories. As you are a movie buff, you review movies very frequently and your movie review posts are comparatively more. You want to organize them separately by genre or by language. You want your followers to be able to browse your movie reviews by genre or language, each indexed on a different page.

To meet this need (or a similar need), you have to create custom taxonomies and then use them to create index pages.

##Creating Custom Taxonomies

The first question that arises is why can't the in-built categories be used?

Well, categories can be used, if your site had only movie reviews and you wanted to organize them by only one criterion, i.e., genre. In this case, categories can replace genres and you can go ahead and use categories. If you do not want genres to be hierarchical, then you can use tags instead.

But, when we want to organize content by more than one criterion, then custom taxonomy is the way forward.

You can create Custom Taxonomies by two different methods: the non-coding method is to use a plugin and the other method is by adding a small piece of code. You can choose either one based on your needs.

Let us look at the non-coding method first and create a custom taxonomy using a plugin.

###Custom Taxonomy Using Plugins

####Installation

There are several plugins available to create custom taxonomies. While you may try any of those, let us look at one of them, Types.

Login to your WordPress site as admin.

From the left sidebar, click on Plugins->Add New and search for Types. From the results, click on Install Now next to the Types plugin.

After successful installation of the plugin, click on Plugins->Installed Plugins and click on the Activate link below Types in the listing.

####Creating a custom taxonomy 'Genre'

Now, let us create a custom taxonomy Genre. From the left sidebar, click on Types->Custom Taxonomies.

In the screen above, click on the blue button Add New Taxonomy. In the Add New Custom Taxonomy screen that appears, fill in the details in the Name and description section. First, the plural name of the taxonomy - Genres, then the singular name - Genre, the slug (the url-version of the taxonomy name) - genre and a description that you feel fit.

The next section is Labels. Here, you may choose to customize the strings used or leave them as is. The default values suit the purpose. The %s in the strings will be replaced by the taxonomy name, in our case 'genre'. So, in the image below, Search %s means that the label will contain Search genres for the genre taxonomy.

The last section is Options. These options are advanced settings which you may not use most of the time. But, if you need to, here is a brief note on each.

  • Hierarchical or Flat radio buttons - You have to decide whether your custom taxonomy is hierarchical, i.e., some of the terms in the taxonomy have a parent relationship with some others. This is similar to the Category of a post. A flat taxonomy is non-hierarchical, similar to the Tags of a post. For the Genre taxonomy, choose hierarchical.

  • Rewrite - This option is used to rewrite the permalinks according to the settings in the Settings->Permalinks section of the left sidebar.

  • Allow permalinks to be prepended with front base - In conjunction with a custom prepend slug that can be specified in the input box above this option, this is used. For example, you can have /movies/ as the custom prepend slug, so that all the permalinks will have /movies/ followed by the post link.

  • Hierarchical URLs - When there are hierarchical taxonomies, this will ensure that the URLs for specific taxonomy items are also hierarchical. For instance, in our example, we may have /genre/fiction/historical-fiction/

  • show_ui - set this to have an UI displayed to manage this taxonomy. In our case, if this option is set, the admin dashboard will have a Posts->Genres option on the left side bar, from where one can manage the Genre taxonomy.

  • show_in_nav_menus - set this to allow this taxonomy to be added to menus. In our case, if this is checked, on clicking Appearance->Menus from the admin dashboard, you can see the Genres listed so that you can add one or more of them to the menus.
  • show_tagcloud - shows a tag cloud widget in the Widgets section. In our case, if this is checked, on clicking Appearance->Widgets from the admin dashboard and dragging the tag cloud widget to any of the sidebar areas, the dropdown for Taxonomy will contain Genres as well.
  • show_admin_column - Shows a column in the list of posts for the particular taxonomy and its values. In our case, if this is checked, on clicking on Posts->All Posts from the admin dashboard, the list of posts will have a column titled Genres.
  • query_var - This is an advanced option. It should be checked to enable customization of queries and the custom query string can be specified in the input box below this.

  • update_count_callback - Another advanced option, which can be enabled to set extra functionality when a post is updated with a particular taxonomy item and the count should be updated.

  • Hide taxonomy meta box - In the post editor, the metabox will not be shown if this is checked. Displaying a metabox is the default behaviour.

  • meta_box_cb - If you want to change the default metabox display function, you can use this input box to specify the function name.

On the Visibility section on the right side, let the default option be. If you choose Hidden, you will not see the taxonomy listed under Posts-> on the left sidebar.

In the Select Post Types section, check on Post. Here we assume that all the movie reviews will be posts. You can choose Page and Media if the particular taxonomy that you are building is applicable to these post types as well.

Click on the blue button Save Custom Taxonomy.

We are done for the moment. In future, if you want to make changes, you can click on Types->Edit Custom Taxonomy from the left sidebar.

You can test this by checking for presence of Posts->Genres link on the admin dashboard. Check the navigation menus for the presence of Genres.

Add genres to posts

When you open the post editor for an existing post, or add a new post, the post editor has a new metabox with the taxonomy name. In our case, a Genres metabox is displayed. You can click on the +Add New Genre link to add one or more genres.

Check on the genres from this metabox to add one or more genres to each review post.

If you have already published several posts for which you want to add genres, you can go to Posts->All Posts, choose multiple posts from the listing and do a bulk edit of the posts. Check against the posts, Choose Edit from the Dropdown box on top and click on Apply. You get a screen like below. Add genres in bulk and click on Update.

Quick editing of genres from the list of posts is also possible. Click on Posts->All Posts. Hover on any post in the list and click on the Quick Edit link that appears below. You can see a Genres box. Change or add genres quickly to posts using this feature.

After adding genres to a few review posts, you can also add a tag cloud to your sidebar. Click on Appearance->Widgets, drag a tag cloud widget to the sidebar. Choose Genres from the dropdown box and Save the widget. Now look at the sidebar in the site.

Managing the Genre taxonomy

If you have set show_admin_ui in the Options section of the settings, then it is possible to manage the taxonomy items for Genre by clicking on Posts->Genres.

This screen is very similar to the category/tag management screen.

There is a listing of popular genres. You can add a new genre item, by giving it a name and a slug. If it is a hierarchical taxonomy (as we have chosen for Genre), you can also choose a Parent genre. Finally, you can add a description.

You can also search for a genre as the number of genres increase.

There is also a listing of genres already added. You can edit or delete a particular genre from here. Just hover the mouse over the specific genre item to see the options below.

Clicking on View will show all posts that are under the specific genre.

##Custom Taxonomies Through Code

If you are slightly savvy with code, but do not know the specifics of Php or WordPress, you can attempt a lighter mechanism to add custom taxonomy. As you see, the Types plugin has so many more features (like Custom fields, User Fields and Post Types) which we don't need and when we use such feature-rich plugins of which we use just a fraction, it slows down the site.

An alternative is to create the custom taxonomies through code.

For the purpose of illustration, let us create another custom taxonomy for the movie reviews, Language.

Instead of writing the code for custom taxonomy, one can get the code generated using the taxonomy generator tool. Head to this link. There are two menus here. We shall be concerned only with the secondary menu.

Click on Taxonomy. The options here are very similar to the one in the options page of the Types plugin. Fill the taxonomy key (taxonomy name, 'language') and its singular and plural forms. Choose the post types, comma-separated if they are more than one. For language, we shall type only post. Choose whether it is hierarchical. As language need not be a hierarchical, choose 'no'.

Click on Labels. This is again similar to the labels option we had in the Types plugin. But, instead of the %s that was the default value that automatically substituted the taxonomy name in the plugin, here, we need to manually change the occurrences of Item to Language. For example, change New Item Name to New Language Name.

The next three tabs is Visibility, Query and Permalinks are similar to the options we described in the plugin. Refer to the options in the plugins section for the details.

Visibility

Query

Permalinks

The Capabilities tab refers to who can manage this taxonomy. This is to ensure that certain roles can only make changes. The defaults are fine.

The last tab is Other, which has the Update Count Callback, discussed in the plugin options.

After choosing your options, click on the Update Code green button to generate the code for creating the new taxonomy. Copy the code generated below.

Open your code editor by clicking on Appearance->Editor. From the list of files shown on the right, click on functions.php. Paste the code at the end of the file.

Click on Update File.

You are done. This achieves the same result without using a plugin.

Test it, by looking at Posts->Languages link, the navigation menus, the tag cloud widget and the admin column.

Add language to review posts using the metabox that appears in the post editor. The metabox is different from that of the Genre taxonomy because Language is non-hierarchical. It appears similar to the Tags metabox.

##Creating an Index Page

Adding a custom taxonomies Genre and Language to all review posts is done. But what is the use of such organization if it cannot be presented to the reader. So, now, let us create an index page where the reader can browse the movie review posts by genre.

The following section is of slightly technical nature. If you want to leave out the nitty-gritties, skip it and go directly to the Code section, that follows the next section.

###Creating the page template

To display the posts by a particular taxonomy, we need to create a page template first.

For this, firstly, we have to get all the terms of the particular taxonomy. We use get_terms function for the same.

    // get all the terms of the genre taxonomy
    $myitems = get_terms('genre', 'orderby=asc&hide_empty');

The above bit of code returns all the genres in the "Genre" taxonomy into $myitems.

Now we need to go through each genre and list out the posts that are of the specific genre. We use a foreach to loop over the genres. For each genre, we create a custom WordPress query and query the database.

    foreach ($myitems as $item) {
        $args = array('genre' => $item->name,'posts_per_page'=>-1);
        $my_posts = new WP_Query($args);
    }

The posts_per_page is set to -1 so as to retrieve all posts that fit the criteria. Now if the query does return some posts, we need to list out the post titles.

    if($my_posts->have_posts()) : 
        echo $item->name; // Print the genre, e.g., Humour
        while($my_posts->have_posts()) : $my_posts->the_post(); 
            the_title(); // print the title of the post.
          endwhile; 
    endif;

We're almost there. It is more reader-friendly to include links to the posts instead of displaying the titles. So, let us alter the the_title(); line a bit so that we include the permalink.

    if($my_posts->have_posts()) : 
        echo $item->name; // Print the genre, e.g., Humour
        while($my_posts->have_posts()) : $my_posts->the_post(); ?>
            <div class="entry-content"> <a href="">
             </a></div> // print the title of the post.
        <?php endwhile; 
        endif; 

Now, we need put all these things together in the right place.

###Code

The code for the twenty-fifteen theme is shown here. If you are using some other theme, it is for you to figure it out with a little bit of exploration and experimenting.

In your wp-content/themes/twenty-fifteen/ directory, make a copy of the page.php file and rename it to genres-page.php.

Open this file and you can see something similar to the image below. The portion selected in gray is the portion we shall replace with the above code.

Remove this code. Replace it with the following code.

    <article >
    <header class="entry-header">
    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

<div class="entry-content"> $item->name,'posts_per_page'=>-1); $my_posts = new WP_Query($args); if($my_posts->have_posts()) : ?> <h3> name; // Print the genre, e.g., Humour ?> </h3> have_posts()) : $my_posts->the_post(); ?> <div class="entry-content"> <a href=""> </a></div>

</div> </article>

Save the file.

Now, we have created a template for listing out the review posts by genre. You could create a similar one for language as well by using the code below.

    <article >
    <header class="entry-header">
    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

<div class="entry-content"> $item->name,'posts_per_page'=>-1); $my_posts = new WP_Query($args); if($my_posts->have_posts()) : ?> <h3> name; // Print the language, e.g., French ?> </h3> have_posts()) : $my_posts->the_post(); ?> <div class="entry-content"> <a href=""> </a></div>

</div> </article>

###Creating the Browse by Genres Page.

The last bit of glue to add is to create the actual page from the template.

Before doing that, reopen the genres-page.php file. At the top, there are a few lines of comment giving details about the file.

Add the following line here.

    * Template Name: Genres-template

Save the file and close it.

Now, on the WordPress admin dashboard, click on Pages->Add New from the left side bar to create a new page. Type the title as Browse by Genres. Now, on the right side, in the Page Attributes metabox, you will find an extra field called Template with a dropdown box. The Genres-template will be listed in this dropdown box, choose this and then Publish the page.

View the Page and it will look similar to the one below.

It does look a little plain, but it has the functionality for an index page where one can browse by genres. You can add some colour by working on your CSS, if you so wish.

Create a page for the languages taxonomy with a languages template, following the same method.

###Add the index pages to menu

Optionally, you can also add these pages to your menu. Click on Appearance->Menus. Check on the Browse by Genres and Browse by Languages pages. Now, click on Add to Menu button. You will see the pages being added to the Menu Structure on the right. Now click on Save Menu.

Visit the site and you will see the two pages on your Menu. Now, your readers have an easy way of browsing your movie reviews either by genre or by language.

That was an example of what can be done with custom taxonomies in WordPress to make organization of your site content easy.

Using Wordpress? Get our lightening fast Wordpress Optimised Hosting from £1.60 / month

View Plans