Add custom taxonomies to a WordPress post

A custom taxonomy was required for Locomotive types,these are categorized by wheel arrangement, for example 0-4-0 means there are four wheels with no leading or trailing axle.

You can create custom taxonomies as either a plugin or in the themes functions.php file. the functions file was chosen here as the owner wanted the custom taxonomy to be part of the site and not a plugin.

Custom taxonomy diagram
It can be difficult to visualize the relationship between all the parts of a taxonomy in WordPress, the diagram below should make this clearer.

When a taxonomy is created in functions.php several things are added to the admin section of a WordPress site.

Code for custom taxonomy
Two things happen when creating a custom taxonomy, and Add action() hook is used to run a function when the post initialises and the new taxonomy is registered (stored) in the database.An array is used to store the settings for the taxonomy, for example whether it is hierarchical or not.

A labels array stores setting for the add new term page for the new taxonomy, this includes settings for the text Shown on the page.

add_action( 'init', 'create_locomotive_hierarchical_taxonomy', 0 );
function create_locomotive_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Locomotive categories', 'taxonomy general name' ),
'singular_name' => _x( 'Locomotive', 'taxonomy singular name' ),
'search_items' => __( 'Search Locomotives' ),
'all_items' => __( 'All Locomotives' ),
'parent_item' => __( 'Parent Locomotives' ),
'parent_item_colon' => __( 'Parent Locomotives:' ),
'edit_item' => __( 'Edit Locomotive' ),
'update_item' => __( 'Update Locomotive' ),
'add_new_item' => __( 'Add New Locomotive' ),
'new_item_name' => __( 'New Locomotive Name' ),
'menu_name' => __( 'Locomotive categories' ),
);
register_taxonomy('locomotives',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'Locomotives' )
));
};

Custom taxonomy template file
Any link from one of the custom taxonomy’s terms (categories within the locomotive taxonomy) will use the taxonomy- locomotive.php file to display a list of posts within that category (term).

You can copy the archive.php file, rename it taxonomy- locomotive.php and customise any text.

To add a category list from the new taxonomy to a post use the code below.Note some of the classes used refer to a Bootstrap stylesheet.

<!-- Show locomotive categories-->
<?php query_posts('posts_per_page=4');?>
<div class="list-group">
<p class="list-group-item"> Locomotive type categories</p>
<?php while(have_posts()): the_post();?>
<p class="list-group-item">
<?php the_terms( $post->ID, 'locomotives' ); ?>
</p>
<?php endwhile; wp_reset_query();?>
</div>
<!-- end div list group 2-->

Conclusion
A custom taxonomy enables you to add specific categories under a grouped heading that can then use a targeted archive template file.

Compartmentalized information is easier to manage and gives access to a site specific schema.