I need help with taxonomy templates. Can find right solutions.

So i have created taxonomy named “products”, added categories and products, i have 3 views:

  1. Category (www.example.com/products/)
  2. Product list (www.example.com/products/category)
  3. Product details (www.example.com/products/category/product-name)

I cant find correct template file name for 1. Category view. It goes by index.php

  1. Category – index.php
  2. Product list – taxonomy-our_products.php
  3. Product details – single-products.php

How should i do this? I dont want to use index.php due issue if i create another taxonomy.

Here is my code:

function products_taxonomy() {  
    register_taxonomy(  
        'our_products', 
        'products',  
        array(  
            'hierarchical' => true,  
            'label' => 'Products Category',
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'products',
                'with_front' => false
            )
        )  
    );  
}  
add_action( 'init', 'products_taxonomy');

function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'products')
        return $link;

    if ($cats = get_the_terms($post->ID, 'our_products'))
        $link = str_replace('%our_products%', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

add_action( 'init', 'register_productpost', 20 );
function register_productpost() {
    $labels = array(
        'name' => _x( 'Products', 'my_custom_post','custom' ),
        'singular_name' => _x( 'Product', 'my_custom_post', 'custom' ),
        'add_new' => _x( 'Add New', 'my_custom_post', 'custom' ),
        'add_new_item' => _x( 'Add New Product', 'my_custom_post', 'custom' ),
        'edit_item' => _x( 'Edit Product', 'my_custom_post', 'custom' ),
        'new_item' => _x( 'New Product', 'my_custom_post', 'custom' ),
        'view_item' => _x( 'View Product', 'my_custom_post', 'custom' ),
        'search_items' => _x( 'Search Product', 'my_custom_post', 'custom' ),
        'not_found' => _x( 'No Product found', 'my_custom_post', 'custom' ),
        'not_found_in_trash' => _x( 'No Product found in Trash', 'my_custom_post', 'custom' ),
        'parent_item_colon' => _x( 'Parent Product:', 'my_custom_post', 'custom' ),
        'menu_name' => _x( 'Products', 'my_custom_post', 'custom' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Products',
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
        'taxonomies' => array( 'our_products'),
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array('slug' => 'products/%our_products%','with_front' => FALSE),
        'public' => true,
        'has_archive' => 'products',
        'capability_type' => 'post'
    );  
    register_post_type( 'products', $args );
}  

Read more here: WordPress Taxonomy template structure. (Category / List / Products)


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: