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 createsingle-music.php
for displaying the music alone right?
That is correct.
and
taxonomy.php
for displaying the single taxonomy content/value likerap
,tupac
,greatest-hits
right?
That is also right.
but how about if I want to display
artist
,album
andgenre
? Like If want to display allartist
on a page using the custom taxonomyartist
? 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: