This particular bit of code stemmed from a current project that I am working on.
The issue at hand is that I have a number of attorneys each who specialize in different practices of law. Each lawyer has their own page with a list of their areas of practice.
This seemed like the perfect opportunity to leverage the custom taxonomy feature in WordPress. So I set up a custom taxonomy called “Areas of Expertise.” Here is the code to register the taxonomy which goes in your functions file.
register_taxonomy( 'expertise', 'page', array( 'hierarchical' => true, 'label' => 'Areas of Expertise', 'query_var' => true, 'rewrite' => true,'show_in_nav_menus' => false ) );
I won’t explain everything that is going on there – if you want a good intro on custom taxonomies read the article on tutsplus.
So now the custom taxonomy is showing up on each page and I can check off all the areas of practice just like you would do for categories.

So now I need to be able to display each area of practice on the individual attorney page but not only that I also need to link it to the specific page that talks about that area of practice the firm offers. So if Daniel Martin specializes in School Law I need Daniel Martin’s page to list School Law and also link to the School Law page. Simple enough it seems but the standard way to print out custom taxonomies not only prints out the taxonomy, it also includes a link to the taxonomy template page. So for each custom taxonomy you create WordPress also “creates” a template page for that custom taxonomy. It is the same thing WordPress does with categories and tags. Each time you create a new category or tag WordPress will have a page available to list all the posts associated with that category or tag.
My problem is that I need that custom taxonomy to link to a specific page I created not the general taxonomy page WordPress creates. And when using the standard WordPress function get_the_terms, you’re stuck with the link WordPress gives you. So being smart I thought I would name organize my pages to be the same as the link WordPress puts out but that opened up a portal to hell I believe and totally screwed with the entire permalink structure and for all I know almost brought down the entire internet.
So I needed a way to get the custom taxonomy without having WordPress be helpful and supply its own link. Here is the golden code that took me the better part of two google days to figure out.
ID, 'expertise'); echo '
- ';
foreach ($terms as $taxindex => $taxitem) {
echo "
- slug ."'>" . $taxitem->name . " "; } echo '
Alright so let me quickly explain what is going on here. First I get a couple variables that we’ll use later – the site url and all the custom taxonomies associated with name “expertise.”
This code here grabs all the terms for the expertise taxonomy that are associated with this particular post and stores them in an array. So right now I have all the areas my lawyer practices in stored in an array. I now need to get them onto the page.
So I use the foreach loop to go through the array and echo out each item that matches. There are a number of things you can echo with this array including the slug and the name of the taxonomy. If you want to see exactly what you can echo include this code and look at the items in the brackets – those are the things you can echo.
print_r($terms);
I needed to echo the slug which I used to get the exact url of the page for the area of practice and of course the name of taxonomy itself. And this is the result!

Instead of
$siteurl/areas-of-expertise/" . $taxitem->slug, you should probably look into get_term_link().The use case is interesting, are you seeing more clients wanting taxonomies built into their tags? And if so how do clients manage these taxonomies/folksonomies when they want them changed?
Well technically tags are taxonomies – the difference here is that these are completely custom. You could call them tags or categories if you wanted to, but WP sees them as a completely separate entity. What I’ve been seeing is that clients are now becoming more comfortable with managing their own site and as the CMS’s build in new features we can use them to allow the clients to do this.
So in this particular case the client sees a meta box in the admin when they are editing the page that asks them to check off the areas in which the attorney practices. Its very easy and user friendly to manage.
My librarians might slightly disagee with you re tags=>taxonomy
but yes, as a dev I would agree with you, atom.label=>atom.label.
Though in looking over your code I’m wondering how you would react (as a dev) to ascribing namespaces to the header and declare the tags as URIs (so they can be WebScale disambiguated). What I’m getting at, is if organisation like law firms are starting to care more about categorising information (on par with search), wouldn’t they also potentially be interested in essentially sharing their taxonomies (via URIs) with other law firms so economies of scale could be acheived in ascribing posts to common ~sector wide vo-cab terms? <- technically an onotlogy in #linkeddata speak at this scale?
I'm mostly interested as my organisation is trying to get more "P…" developers generally interested in the concept of linkeddata and this is a clear use case, so any feedback (your initial keen jerk reactions) you have on the topic would be appreciated.
Not sure if I’m understanding where you are coming from, but when in using custom taxonomies I’m strictly using them as a way to make content management easier for the end user and to be able to place and relate content on a specific page.
In WordPress technically every tag, category, or custom taxonomy, has their own url – this was one of the problems I ran into because having the same page names and custom taxonomies would reek havoc with the permalinks and how WordPress redirects urls.
The law firm in question had no say in how the information was categorized in WordPress other than the fact that they wanted each lawyer to have displayed on their page what areas of practice they specialized in.
Bonus. Good find!
Thanks for the two days! I only had to Google for two hours to find this article with the solution I needed
THANK YOU.
This little trick was crucial for me to link my custom post taxonomies to my custom post types. Worked perfect!
THANK YOU. This little trick was crucial for me to link my custom post taxonomies to my custom post types. Worked perfect!