How to create a custom template for a custom taxonomy?

I have the following below which I have used to create a custom post type and a custom taxonomy.

Within the products section I’ve created the categories “monitors” & “consumables”.

I have then created the template taxonomy-monitors.php, is that correctly named for the monitors category? Also what is the url I need to visit to see only the monitors category using that template?

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'products',
        array(
            'labels' => array(
                'name' => __( 'Products' ),
                'singular_name' => __( 'Product' )
            ),
        'capability_type' => 'post',
        'supports' => array('title','editor','comments'),   
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'products' ),
        )
    );
}

function news_init() {
    // create a new taxonomy
    register_taxonomy(
        'products',
        'products',
        array(
            'label' => __( 'Product Categories' ),
            'sort' => true,
            'hierarchical' => true,
            'args' => array( 'orderby' => 'term_order' ),
            'rewrite' => array( 'slug' => 'products-category' )
        )
    );      
}
add_action( 'init', 'news_init' );

UPDATE
enter image description here

3 s
3

Templates

See the Template Hiearchy for a more detailed break down of how WordPress chooses the template.

For a taxonomy term slug (‘monitors’ your example) in the taxonomy taxonomy (e.g. ‘products’) WordPress will try to use the following templates (in this order)

taxonomy-{taxonomy}-{slug}.php
taxonomy-{taxonomy}.php
taxonomy.php
archive.php
index.php

For your ‘monitors’ taxonomy term page, WordPress will use

taxonomy-products-monitors.php

if it exists. If it doesn’t, then for that taxonomy it will fallback to

taxonomy-products.php

and so on.

Permalinks

The following url should point to the ‘monitors’ products page:

 www.example.com?products=monitors

You have also specified an url rewrite, so assuming the rewrite rules have been flushed and there isn’t a clash, the following should also work

 www.example.com/products-category/monitors

Leave a Comment