4

Is it possible to create a template file specific for a custom taxonomy?

For example, I have music Custom Post Type and it has three taxonomies artist, album, and genre. So the hierarchy would be like this:

music               // custom post type
├── artist          // taxonomy
│   ├── snopdogg    // taxonomy content
│   ├── tupac
│   └── biggie
├── album
│   ├── greatest-hits
│   ├── murder-death-kill
│   └── all-is-well
└── genre
    ├── rap
    ├── hip-hop
    └── alternative

For the custom post type, I'll create a single-music.php for displaying the music alone, right?

And taxonomy.php for displaying the single taxonomy content like rap, tupac, greatest-hits right?

But what if I want to display artist, album and genre? For example, if want to display all artist on a page using the custom taxonomy artist?

What file should I add? or is it done manually? like, I will literally add a page called artist and then that's it? If yes, is there a way to automate it? Like how single-post.php, taxonomy.php, archive.php works?

1

1 Answer 1

4

Is it possible to create a file specific for custom taxonomy itself?

Yes, it's possible in WordPress.

So for the custom post type (music) I'll create single-music.php for displaying the music alone right?

That is correct.

and taxonomy.php for displaying the single taxonomy content/value like rap, tupac, greatest-hits right?

That is also right.

but how about if I want to display artist, album and genre? Like If want to display all artist on a page using the custom taxonomy artist? What file should I add?

You can create different custom taxonomy template files based on the following file name priority convention. WordPress looks for these template files and uses the available file with the highest priority.

taxonomy-{taxonomy}-{term}.php (highest priority for an individual custom taxonomy term. e.g. taxonomy-artist-snopdogg.php)
    │
    └──→ taxonomy-{taxonomy}.php (highest priority for a custom taxonomy archive. e.g. taxonomy-artist.php)
        │
        └──→ taxonomy.php
            │
            └──→ archive.php
                │
                └──→ index.php (lowest priority, this is used if no other template file is found)

For exampe, if the URL you are visiting is a custom taxonomy term URL of artist taxonomy, and snopdogg taxonomy term (e.g. example.com/music-information/artist/snopdogg [in this URL, music-information is just a custom url-prefix I've assumed for your custom taxonomies]), WordPress will look for taxonomy-artist-snopdogg.php file. If this file is not found, then WordPress will try to load taxonomy-artist.php file. If even this file is not found, then WordPress will keep trying to load other lower priority files until it reaches index.php.

Similarly, if the URL you are visiting is a custom taxonomy archive URL of artist taxonomy (e.g. example.com/music-information/artist), then WordPress will try to load the taxonomy-artist.php file.

So, if you want to display all available artist on the artist custom taxonomy archive page, you can create a file called taxonomy-artist.php, and use the get_terms function to retrieve all the terms associated with the taxonomy and display them on the page. For example:

$args = array(
    'taxonomy' => 'artist',
    'hide_empty' => false,
);
$artists = get_terms($args);

echo '<ul>';
foreach ($artists as $artist) {
    echo '<li><a href="' . get_term_link($artist) . '">' . $artist->name . '</a></li>';
}
echo '</ul>';

For more information, you may refer to the following links:

  1. WordPress Template Hierarchy

  2. Taxonomy Templates

  3. Custom Post Type Template Files

2
  • 1
    hi @Fayaz, thanks for the detailed answer, it did help me clarify some confusion before, now that there is taxonomy-{taxonomy}-{term}.php, is there anyway to automate it? instead of literally/manually creating a taxonomy-{taxonomy}-{term}.php? Jan 24 at 16:35
  • 1
    Yes, of course. WordPress falls back to less specific template file if taxonomy-{taxonomy}-{term}.php is not available. So, you can simply create taxonomy-{taxonomy}.php and in that file query the current term and show data accordingly. You can do more generic implementation with just a single taxonomy.php to handle them all. But the more generic you go, the more condition you'll have to program to handle all possible cases within that template file.
    – Fayaz
    Jan 24 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.