phpwordpressviewcategories

How to use template for a WordPress category?


Here is my code:

$config = array(
    'labels'            => $genre_labels,
    'hierarchical'      => true,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'htfiles-genre' ),
    );

register_taxonomy('htfiles_genre', array('htfiles'), $config);

I create in theme phpfile :

Category-htfiles-genre.php
category-{slug}.php

– If the category’s slug is htfiles-genre, WordPress should look for category-htfiles-genre.php, but he doesn't.

Thanks for any help!


Solution

  • The code you've posted is used to register a custom taxonomy which follows a different naming structure to regular categories.

    To display a template file for the taxonomy, you need to use taxonomy-{taxonomy}.php. If you wanted to create a template for a given term in that taxonomy, you would use: taxonomy-{taxonomy}-{term}.php.

    Your filename should be: taxonomy-htfiles_genre.php.

    Documentation: https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-taxonomies