STEFAN CROWE
PHP, MySQL and JavaScript Developer. WordPress fanatic.

Adding custom categories to WordPress

4th February 2012 15:42
Category: WordPress — Written by

Note: You will be required to make some basic changes to code to carry out this tutorial – make sure you create backups before making any changes to your website code

Most people don’t realise that WordPress isn’t just a blogging paltform, but that it is also a powerful content management system (CMS) in general. One of the many key features that enhances WordPress’ capabilities as a CMS is the ability to add your own categories and tags. This allows you greater control over the navigation and layout of your content throughout the WordPress site.

An example of when custom taxonomies (a fancy way of saying categories and tags) may be of use is if you imagine a sports website. You might use the normal categories to distinguish between types of posts (e.g. news, match reviews, opinion pieces, etc.), but what if you want to associate particular players or teams to individual posts?

This is where custom categories and tags come into play; with this functionality you can create a whole new category for your teams, and you could even add a new tags section for players.

Adding a custom category

There should only be one file you need to edit for this process – it is called functions.php and is located in your theme directory (/wp-content/themes/<your theme>/functions.php). Don’t worry if there isn’t a file by this name here, simply create a plain text file and rename it to functions.php.

Add the following code to the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//create new category taxonomy "Teams"
function create_new_taxonomies()
{
    // Hierarchical (e.g. categories)
    $labels = array(
        'name' => 'Teams',
        'singular_name' => 'Team',
        'search_items' =>  'Search Teams',
        'all_items' => 'All Teams',
        'edit_item' => 'Edit Teams',
        'update_item' => 'Update Team',
        'add_new_item' => 'Add New Team',
        'new_item_name' => 'New Team Name'
    );  
 
    register_taxonomy(
        'teams', 'post', array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'team' )
    ));
}
add_action( 'init', 'create_new_taxonomies', 0 );
//create new category taxonomy "Teams"
function create_new_taxonomies()
{
	// Hierarchical (e.g. categories)
	$labels = array(
		'name' => 'Teams',
		'singular_name' => 'Team',
		'search_items' =>  'Search Teams',
		'all_items' => 'All Teams',
		'edit_item' => 'Edit Teams',
		'update_item' => 'Update Team',
		'add_new_item' => 'Add New Team',
		'new_item_name' => 'New Team Name'
	); 	

	register_taxonomy(
		'teams', 'post', array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'team' )
	));
}
add_action( 'init', 'create_new_taxonomies', 0 );

If you are happy with not knowing the intricacies of this code, then simply change references of ‘teams’ and ‘team’ to suit your custom category and you’re good to go.

Now, let’s take a look at this code in finer detail, starting (somewhat confusingly) with the last line:

1
add_action( 'init', 'create_new_taxonomies', 0 );
add_action( 'init', 'create_new_taxonomies', 0 );

This tells WordPress to call the function create_new_taxonomies() upon loading. This function (seen on line 2) contains the code to create our custom category taxonomy. This is split into two parts, the first is a list:

1
2
3
4
5
6
7
8
9
10
$labels = array(
    'name' => 'Teams',
    'singular_name' => 'Team',
    'search_items' =>  'Search Teams',
    'all_items' => 'All Teams',
    'edit_item' => 'Edit Teams',
    'update_item' => 'Update Team',
    'add_new_item' => 'Add New Team',
    'new_item_name' => 'New Team Name'
);
$labels = array(
	'name' => 'Teams',
	'singular_name' => 'Team',
	'search_items' =>  'Search Teams',
	'all_items' => 'All Teams',
	'edit_item' => 'Edit Teams',
	'update_item' => 'Update Team',
	'add_new_item' => 'Add New Team',
	'new_item_name' => 'New Team Name'
);

This list is an array of variables, these are to replace the text labels used throughout WordPress to let users know how to deal with your new taxonomy. I have included a few examples in the code, the full list can be seen on the official WordPress Codex page for register_taxonomy. Finally, with our labels prepared, we proceed to the final stage of the process:

1
2
3
4
5
6
7
8
9
register_taxonomy(
    'Teams', 'post', array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'team' )
    )
);
register_taxonomy(
	'Teams', 'post', array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'team' )
	)
);

This tells WordPress to create a new taxonomy. The first two parameters: 'Teams', 'post' are the name for the new taxonomy and the post type (in this case just normal posts, you could instead put ‘page’ to have it usable on pages or other custom types).

The third parameter is an array of options, here you can customise the functionality of the new taxonomy. For the code above, here is a quick explanation of each option in respective order:

  • hierarchial: if true it behaves like categories, otherwise it acts more like tags
  • labels: here we insert the labels list we created earlier
  • show_ui: whether or not to display management controls for this taxonomy in your admin panel
  • query_var: for advanced users – allows you to easily call posts of this taxonomy type using it’s name in custom post queries
  • rewrite: allows the taxonomy to have it’s own seo-friendly permalink structure (www.yourwebsite.com/<slug>)

Adding a custom tag taxonomy

You can also make the taxonomy emulate tags rather than categories by changing the line 'hierarchical' => true to 'hierarchical' => false.