Building Dynamic Taxonomy Pages With WordPress
Tutorial Details
- Program: WordPress
- Difficulty: Medium
- Estimated Completion Time: 1 hour
Ever wondered how you can build a library that’s as flexible as the content you’re publishing? Today I’ll walk you through how I setup dynamic taxonomy pages for the musicians that on my music blog, but you can apply these same techniques in a variety of different ways. This tutorial shows you how you can build a dynamic record for anything you write about.
I run a large music website. In order to keep track of all the times that different artists were mentioned and in what context, I wanted to create a page for each of them. By creating new taxonomies in WordPress, I was able to do this by just tagging a band/artist in a WordPress post. As well as this, I could also dynamically create affiliate links and draw in biographical content from a site like Last.FM.
This tutorial will show you how to setup your taxonomy, build a template for it and then implement a dynamic generation of content and links to the artist in your taxonomy elsewhere on the web
Step 1 Setting Up Your Custom Taxonomies
To get started, you’ll need to add the custom taxonomies function to your WordPress install. To do this, just open up your functions.php file from your theme folder, scroll all the way to the bottom and then turn the function on like this:
/* *Adds the custom taxonomy function to our functions.php file. */ add_action( 'init', 'build_taxonomies', 0 );
Once you’ve done that, you can start adding the taxonomies that you want. For this tutorial, we’ll be adding an artist taxonomy which will allow us to tag bands in posts that we write and have WordPress pull out a load of information about that band or singer without us ever needing to do anything else. That being said, let’s go ahead and throw in the ‘Artist’ taxonomy just beneath the taxonomy function that we just added.
function build_taxonomies() {
register_taxonomy( 'artist', 'post', //Let WordPress know that the artist taxonomy has posts
array(
'hierarchical' => false,
'label' => 'Artists', // This tells WordPress how to label the various user interface outlets for the artist taxonomy
'query_var' => true,
'rewrite' => array( 'slug' => 'artist', 'with_front' => false ))
);
}
You don’t have to add an artist taxonomy, you could add any taxonomy you wanted to. We’re just doing it for the purposes of this tutorial. For a full list of arguments and options that you can use in the array, check out the Codex and add or take away whatever you want. Just as long as you leave in the parts that are commented here, you’ll be fine.
Now let’s open up our WordPress admin panel and write a post. Notice that a new ‘Artist’ box has been added to both your admin menu and your ‘write post’ views. This means WordPress has recognised your new function. Brilliant!

Once you click ‘Add’ on on the artist tag box and press publish, WordPress will automatically create a new artist called ‘Whatever you named your band’ – for this tutorial I’ve chosen to use a local band called Kutosis.
Some people will say that you could just have a category page to do the same thing. They’re kind of right but we’re about to bump into two problems if we do that:
- If we write about a good number of bands, our WordPress install will get really messy soon if we create one category for each band
- If we want to include dynamically created content for each artist, we’ll be swimming in unnecessary code
Using custom taxonomies helps us avoid these problems and allows really scalability in our website. It also means that by providing an artist tag, we can do a lot of cool stuff with API calls to music websites.
But before we get excited about all of the possibilities, we need to work out how to display the information on our page
Step 2 Setting Up Our single.php Template
Now that we’ve specified our post contains references to a certain band/artist, we need a way to indicate that to readers of the post. The way to do that is to add a simple couple lines of code to show links to all of the artists who we’ve talked about to the single.php file which WordPress uses to display individual posts.

See the link? Just where it says ‘In this post’ – that link is accomplished by adding the following code to our single.php file. I’ve chosen to add it just above the part of my template that calls the comment template:
ID, 'artist', 'In this post: ', ', ', '' );
if ( '' != $artist_list ) {
$taxo_text .= "$artist_list\n";
}
// Now we need to tell WordPress exactly how we want to display the tag
if ( '' != $taxo_text ) {
?>
// This just let's WordPress know that there's an else argument. It will remove the 'In this post' if no artists have been tagged.
}
?>
// You don't need the following line. I have left it here to show you where I placed my code.
This section of code tells WordPress to do a few things:
- It tells WordPress to look for any tags that are associated with the post
- Then it specifies that we are interested in the Artist tag
- After that it tells WordPress how we want to display it if it’s there and how to not show anything if there isn’t a tag associated with the post
Step 3 Creating a Taxonomy Template
So now we’ve got a way to link to an archive page which displays all the posts associated with the artist tagged in the post – we could stop there but there’s so much more that we can get out of these powerful little taxonomies. For example, what if we want to monetize our blog and set up affiliate links to buy the artist’s music on iTunes or simply let our readers enjoy our writing with some Spotify tunes from the artist in question? Or even better – if we’ve got curious readers, we could allow them to read a little bit about the artist tagged in the post and never have to write a word ourselves. Exciting right?!
Instead of adding load and loads of conditional code to our archive template and making it ridiculously long, let’s take full advantage of the template hierarchy of WordPress. We’re going to add a file called taxonomy-artist.php which is almost the first file name that WordPress will look for in order to format our information. To learn more about WordPress template hierarchies, head over to the WordPress Codex.
Duplicate the archive/category.php file from your theme and rename it to taxonomy-artist.php (or if you’ve decided to create a different taxonomy, then change artist out for whatever your taxonomy is called) so that you can easily maintain your template structure. Once you’ve duplicated the template/category.php files, you should now strip your new taxonomy-artist.php file of all code other than the bare necessities of your structure. Don’t worry, we’ll add it all back in as we go along.
Once you save, you’ll be left with a bare bones structure like this one:

Having stripped out all of the code that we didn’t need, let’s put in all the code that we do need. Starting right at the top, just below the header of our page, we’ll add what it perhaps the most vital and shortest code and it looks like this:
What that little beauty does is to give us a variable called $term for everything we’re about to do. $term will be the name of our taxonomy. For our example, WordPress will be using the band Kutosis because that’s the act I tagged in my post.
Then we can use it in our template to add in a taxonomy title
name; ?>
We can also use it to pop in a list of posts which have made reference to the artist throughout our site. Note: You’ll have to tag the post yourself because WordPress won’t guess for you.
name; ?> elsewhere on the site
Step 4 It’s API Hour!
Having done all of that, we’ve got something kind of like this:

It doesn’t look too impressive right? That’s because we’re about to really turn up the volume on this band’s page! Let’s start by adding some links to the artist around the web to provide some added value for our readers.
Last FM | Buy Music | Spotify
These three lines of code are examples of places you might want to link to. I’ve added a Last.FM, Spotify and iTunes link. With the iTunes link, if you’re signed up to the affiliate programme, you can add your partnerID into the URL – if you’re not a member, don’t worry, just take it out.
As I said, these are just examples of place you could link to. Anywhere you want to add a link to the artist on the web, just work out how the permalinks to that site are structured and where the artist’s name comes. Then just put the variable we set up into the URL like I did in the above code.
The other great thing about this is that even if the artist has spaces in their name, most sites will be able to interpret that nicely and make sure that you’re directed to the correct place. Just test it out and see what happens. Here’s how our page looks.

Now let’s actually use an API
So far, we’ve just made our links dynamic. What if we want to add some more value for our readers and give them a quick overview of the artist that we’ve been writing about?
The Last.FM API allows us full access to its huge library of music information. This API is probably one of the best out there and so we’re in luck! Let’s grab the artist’s biography from Last.FM’s database.
If you do not already have a Last.FM API key, you will need to register for one by clicking here before you go any further in this tutorial.
Once you’ve got your Last.FM API key, open you taxonomy-artist.php file back up and decide where you want to place the biography. Then take a look at the documentation on the Last.FM website to see that the structure of the API call for artist.getInfo is this: http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=cher&api_key=yourapikeynumber.
Unless you want info about Cher’s musical career on every artist page on your website, you’ll need to change some stuff in the URL. Delete Cher and replace it with our artist variable so that you’ve got this:
http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=".$term->name."&api_key=YOUR_API_KEY
That’s the API call structure that we’ll be using but unfortunately, it’s not as straightforward as pasting it in to a template file and letting WordPress work it’s magical digits. We’ll have to work out a way to tell the page exactly which part of the artist’s information we’re needing. Before we jump ahead of ourselves though, we should set up a cURL function to get the information from Last.FM.
CURL isn’t the only way to do this but most webhosts will be annoyed at you if you turn on the other functions like fopen in your PHP.ini file because it could pose a security risk. To avoid embarrassing error messages when your webhost discovers you using a method non grata, it’s best to go for the following:
name."&api_key=YOUR_API_KEY"; //Begin our curl resource $ch = curl_init($request_url); //Put all the stuff from the page we called into a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Put that info into a nice output $output = curl_exec($ch); // Sort out our memory curl_close($ch); ?>
This is a standard CURL function. It requests the Last.FM archive for our artist, prints it into a curl resource and then outputs it nicely before closing itself.
But of course, we still haven’t told it where we want the description of the artist to go or exactly how to find the description. Let’s do that using SimpleXML.
name."&api_key=YOUR_API_KEY"; //Begin our curl resource $ch = curl_init($request_url); //Put all the stuff from the page we called into a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Put that info into a nice output $output = curl_exec($ch); // Sort out our memory curl_close($ch); //IMPORTANT: This is where we begin to tell how we want the output displayed $xml = simplexml_load_string($output); //Here's where we tell the page the specific place that we want it to print information from $info = $xml->artist->bio->summary; ?>
We don’t want everything in the XML file – we only want to get the artist summary which contains a short version of the bio.
That’s what this bit does: $info = $xml->artist->bio->summary;
Then, the summary is assigned to a variable called $info which we can echo anywhere on our template page leaving us with something that looks like this:

Conclusion
Congratulations! You’ve made a dynamically generated artist page for your favorite band!
You can do all kinds of great stuff with the Last.FM API including pulling in links to the top 5 listened to tracks by an artist, bio pictures etc. The best thing to do is have a play around with the API calls that you can make.
If you would prefer to handle bio images yourself, there are some great plugins. Most notable is the Taxonomy Images plugin by Michael Fields which has a simple way to add custom images to your taxonomy without any coding. If you choose to use that plugin, check out Michael’s FAQ for some really good documentation on how to use it to your needs.


Is there a way to have multiple taxonomies in the title?
Hey Josh,
Not entirely sure what you mean sorry. What did you want to do?
Marc
Hi Marc, I’m referring to the single_cat_title template tag that is used to display the category title. This can be used to display the category title on a category archive page. It also works for a taxonomy. But if you have more than one taxonomy on a post, there seems to be no way to display all of them in the title?
Could you please explain for a novice (sorry) what the difference between <!–?php … ?–> and <?php … ?> ? I notice some times you even use <!–? } ?–> without the “php”. I’ve not seen that before, perhaps revealing my newness to this. These wouldn’t be typo’s would they?
Also, you show $post- – ->ID (i.e. with three dashes, no spaces) – is that something different for the taxonomy functions? Sorry if this is newbie stuff, I have read a lot of code but it isn’t something that I’ve ever noticed before.
I’d say the first thing is something he’s doing to prevent the PHP from executing, though that shouldn’t be a problem within the shortcodes, unless it’s something plugin is doing to him.
The second part makes no sense…
Hey DJ,
<?php is no different than <? on most web hosts, they should still work. <? is just a shorthand version. However, some hosts don't allow <? so to be safe, use he should actually be using $term->. I believe that it’s coming out oddly because of his IDE or something similar. You’ll notice that he’s wrapping his PHP in HTML comments (<!–), Marc may be doing this because of his IDE or for the syntax highlighting feature of the site.
Summary:
<? is shorthand for , not $term—>
Hey Marc,
Cool little tutorial! Nice work.
It might be worth noting that there is a cool WordPress API called the HTTP API http://codex.wordpress.org/HTTP_API which is very funky and it would be great for the API that you’re interacting with at the end of the tutorial.
In particular you’d probably use wp_remote_get http://codex.wordpress.org/Function_API/wp_remote_get. The trouble with your example is that you are relying on CURL and although most hosting setups have CURL enabled there are a few that don’t. The beauty of the WordPress HTTP API is that it will try 5 different ways to send and receive the HTTP request so it’s much more robust and the WordPress guys have done all the tricky coding for us!
Hey Bronson,
That’s ace! Thanks for sharing the link. I’ll take a look at that ASAP. Embarrassingly, I didn’t know WordPress did that. I suppose that’s why I love working with WordPress so much: I just keep learning about how powerful it is!
Cheers,
Marc
Haha yeah WordPress is pretty amazing! I’m constantly learning about new things built into the WordPress core so I’m always happy to share what I’ve learnt with others. The more we all teach each other the better we get and the better WordPress gets as well
Marc Thanks for sharing this I’m new to wordpress,I have small confusion related to taxonomies.Are tags & taxonomies both same
Hey Mark,
No, taxonomies and tags aren’t the same. You could think of the hierarchy of WordPress kind of like an apple tree:
1) The trunk of the tree is the index.php file
2) Then you’ve got loads of different branches like archive.php, category.php and taxonomy.php
3) At the end of each of the branches, there are tags (like the fruit) to help organise the content within the bigger branches.
For a less poetic explanation, here’s a WordPress page explaining the difference: http://codex.wordpress.org/Taxonomies
Our example uses custom taxonomies (artists) and default taxonomies (tags).
Hope that helps,
Marc
Awesome tutorial. Bookmarked!
When you say strip out all code not needed, what exactly do you mean?
Jerry,
For this example, I duplicated the archive.php file from my WordPress theme. That means that there was a lot of code that I didn’t really want on my taxonomy-artist.php template, so I stripped it all out and left only the elements to do with the structure in.
I think that answers your question.
Marc
I’m trying to get this to work, and I’ve got the first two steps completed – but now instead of seeing the artist tag in page I get
Bookmark the permalink.
ID, ‘artist’, ‘In this post: ‘, ‘, ‘, ” ); if ( ” != $artist_list ) { $taxo_text .= “$artist_list\n”; } // Now we need to tell WordPress exactly how we want to display the tag if ( ” != $taxo_text ) { ?> class=”entry-utility”>
// This just let’s WordPress know that there’s an else argument. It will remove the ‘In this post’ if no artists have been tagged.
Leave a Reply
I got this to work using this guide – http://wp.tutsplus.com/tutorials/theme-development/innovative-uses-of-wordpress-post-types-and-taxonomies/
Their code is radically different, does your approach offer some advantage?
Thanks for this, this is making the relaunch of my my music blog awesome
Not able to get this to work for me. I’ve tried the tutorials on a couple sites, and I keep running into the same problem.
I’m on a self-hosted site using a WP template.
I’m getting to the theme php screen with no difficulty.
I feel like I’m copying and customizing the code correctly, though I’m uncertain if possibly something is happening during the copy & Paste action that what’s going out ain’t the same as what went in (ie, auto-spacing or indenting, something small that might be causing big problems).
The only time I get any type of change is when I see, very briefly, the code I copied flashing just behind my dashboard header.
I’m also running a (tiny) music website, and so your example of Artists as a taxonomy is speaking my language. I was looking to create three custom taxonomies: Musician, Label, and Sound.
I’m new to all of this, but I enjoy scouring the internet and grinding through the information that’s there. It’s a fun challenge. However, I’m concerned that maybe I’m running into an obstacle that I might not be able to recognize with my limited knowledge. I appreciate any help that can be given.
Cheers.
I was having problems for a bit until I realized that CURL was not turned on in my local WAMP server. So, if you’re having troubles cutting and pasting, check that. If you’re having trouble doing it on a live site, try making a phpinfo() page to make sure you’ve got CURL active on your live server too!