Creating WordPress Custom Taxonomy Archives

Views Fast-track »

This tutorial explains how to design custom taxonomy archives using PHP and WordPress templates.

You can also display custom taxonomy archives without writing any code using Views plugin.

Custom taxonomy archive are an efficient way of grouping custom post topics in WordPress. In this tutorial, we will show you how to create your own custom taxonomy archives.

There are two ways to display custom taxonomy archives:

  • Using WordPress API and coding with PHP
  • Using Views plugin, which requires no coding

Let’s take a quick look at this type of archive.

Introduction to Custom Taxonomy Archives

Custom Taxonomy Archives list your custom content in the defined taxonomy. The taxonomy used in grouping your content topics can either be a category or a tag.

For example, say you have an invoice content post type that contains information regarding different employees in your company. You might want to group these invoices. One invoice belongs to a regular employee, while the other invoice would belong to a contractor.

Creating custom taxonomies can be easily done using Types.

Grouping topics with taxonomy

You can see that there are two taxonomies added for the “invoices” post type: ‘employee’ and ‘contractor’. The next step is to now assign a taxonomy to your custom post types. This is done by editing the post.

Here, let’s assign ‘Permanent’ to some invoices under the regular “employees” taxonomy.

Employee taxonomy

And assign ‘Temporary’ for “contractors” taxonomy.

Contractor taxonomy

The resulting custom taxonomy archive URL will look like this:

http://www.example.com/employee/permanent/

http://www.example.com/contractor/temporary/

Customizing taxonomy archive template files

If you are using Types in creating your custom taxonomies, WordPress uses the ‘archive.php’ template by default. This is true if you don’t assign a custom template in displaying your custom taxonomy archive. The template is used in rendering the custom taxonomy archive content.

Follow these steps to customize the display of your custom taxonomy archives.

1. Copy archive.php to taxonomy-{taxonomy_slugname}.php

Locate the file archive.php in your theme and save it as taxonomy-{taxonomy_slugname}.php.

The taxonomy slug name can be created using Types. So for the contractors taxonomy, let’s simply name it “contractor”.

Slug name for custom taxonomy archive

The complete template file name will be:

taxonomy-contractor.php

WordPress will now use this template file instead of archive.php to display your custom taxonomy archive.

You can do the same for other taxonomies. Hence, the employee taxonomy will be:

taxonomy-employee.php

2. Customizing the taxonomy archive template

Custom taxonomy archive templates are specific to each taxonomy. You do not need to use any conditional tags to decide whether you are in the employee or contractor taxonomy. If WordPress wants to display the employee taxonomy archives, it will simply use the taxonomy-employee.php template.

This means you can put a specific code that applies to each taxonomy by editing their respective templates.

Example – Customizing the loop

You may also want to customize the loop of your custom taxonomy archives. A good example would be to change the number of posts to be displayed; or change the way the post will be sorted.

Example code to customize taxonomy archive loop
<?php

//Define the function

function customize_customtaxonomy_archive_display ( $query ) {
	if (($query->is_main_query()) && (is_tax('employee')))
		
	$query->set( 'posts_per_page', '3' );
	$query->set( 'orderby', 'title' );
	$query->set( 'order', 'ASC' );
}

//Hook the function

add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );

?>

The above code will check if the main query is executed. It will also check if it’s in the ‘employee’ taxonomy archive.
The code will then display three posts per page, all arranged by title in ascending order.

Example – Adding custom fields to custom taxonomy archives

It is also common to display custom fields in the archive content.

Let’s illustrate this using the Types PHP API and TwentyEleven theme.

Basically, it’s the same as with other types of archives. The only difference is that you should use conditional tags to accurately display the content.

So in the TwentyEleven theme, archive content is controlled using get_template_part function referencing to content.php. If you are already displaying custom fields in your other custom archives, the code will look like this:

Editing TwentyEleven content template to display custom fields
<div class="entry-content">

<?php the_content( __( 'Continue reading <span class="meta-nav"></span>', 'twentyeleven' ) ); ?>

<?php 
			
//Display the contact number in the archive
echo "Contact number:<br />";	
echo types_render_field("contactnumber", array("output"=>"HTML"));				

?>

If you wish to customize the display in your custom taxonomy archive, then use a conditional tag, for example:

Using conditional tag to display custom fields in custom taxonomy archive
<div class="entry-content">

<?php the_content( __( 'Continue reading <span class="meta-nav"></span>', 'twentyeleven' ) ); ?>

<?php 

if (is_tax('employee')) {

//Display email address if the employee taxonomy archive is displayed

echo "Email Address:<br />";	
echo types_render_field("email", array("output"=>"HTML"));
	
} else {
				
//Display the contact number of the invoice in other archives

echo "Contact number:<br />";	
echo types_render_field("contactnumber", array("output"=>"HTML"));				

} 

?>

Customizing Custom Taxonomy Archives with WordPress Archives

Taxonomy archive in grid layout

Views plugin lets you easily design custom taxonomy pages. Using Views GUI, you can choose which taxonomy archive you want to customize. Select the fields to include for each entry and style them using HTML and CSS. Views plugin will put everything together and create the completed custom taxonomy archives.

To customize taxonomy, as well as other WordPress archived, you’ll use an WordPress Archive. Here are the steps to do that:

  1. Choose the archive look to customize
  2. Select the layout for the content on that page
  3. Insert fields and design the appearance using HTML and CSS

To learn more, read about using WordPress Archives. You can experiment and learn this for free on our Discover WP, or jump in and buy Toolset and start using it on your own sites.