New full sized and mini business cards, as well as my holiday greeting cards. Merry, merry! I buy all my [...]
Displaying Custom WordPress Taxonomy List Items Without the Links
April 21st, 2011
Or: How to Strip <A> Tags From get_the_term_list
Okay, this one is a bit obscure and not terribly complex, but it took me a minute to solve, so here it is for the rest of the world.
I was working on a WordPress custom taxonomy as a way to store and display useful information about a post. Specifically, I was creating a “Venue” type to store nightclub names which could then be displayed easily throughout the theme’s code, as well as inserted via shortcode.
Creating the Custom Taxonomy
Creating the taxonomy itself wasn’t really that hard, and isn’t the main subject of this post, but for the sake of completeness, I’ll cover that briefly first.
register_taxonomy('your-custom-taxonomy-name', 'page', array('hierarchical'=>true, 'label'=>'Taxo Name', 'query_var'=>true, 'rewrite'=>true) );
}
In the above code, I’m registering the taxonomy “your-custom-taxonomy-name” to attach to pages (as opposed to posts). I’m declaring it to be a hierarchical type, like WordPress’ native Categories, with the label “Taxo Name” showing up as the label above it in the admin. The “query_var” tells whether it should apply to custom post types, and “rewrite=>true” allows for pretty URLs such as “mysite.com/your-custom-taxonomy-name/item” instead of the default “mysite.com/?your-custom-taxonomy-name=item”.
This declaration in your functions.php file will register the taxonomy with the database and put a new meta box next to your pages.
For more info on custom taxonomies and how they work, check out custom taxonomies in the WordPress codex.
Outputting the Taxonomies Without Links
Now, I wanted to output the taxonomy info into the theme; i.e., I wanted the theme to display the name of the venue in various places. My only problem was that while the most obvious choice for this was WP function “get_the_term_list()“, it outputs the taxonomy terms as links, wrapped in <a> tags. After some mucking around, I discovered two possible solutions.
Method 1: Stripping Out All HTML
The first works quite well if you don’t need to wrap each tag in any kind of code (like list tags or whatnot). Just use “strip_tags()” like so:
$terms_as_text = get_the_term_list( $post->ID, 'your-custom-taxonomy-name', '', ', ', '' ) ;
echo strip_tags($terms_as_text);
?>
This will spit out all your taxonomy terms with commas between them but without any HTML around them whatsoever, no matter what you use for the “get_the_term_list()” parameters because “strip_tags()” will just remove them again. Depending on how you’re planning to display your terms, this could be all you need.
Method 2: Starting Without Any HTML Tags and Adding the Ones You Want
On the other hand, I also had another taxonomy I was creating, called Top Features, for storing features of each venue for easy display. I wanted them to output into an unordered list, which, while supported by “get_the_term_list()”, would be removed by “strip_tags()”.
My solution was to use get_the_terms instead.
if (get_the_terms($post->ID, 'your-custom-taxonomy-name')) {
$taxonomy_ar = get_the_terms($post->ID, 'your-custom-taxonomy-name');
$output = '<ul>';
foreach ($taxonomy_ar as $taxonomy_term) {
$output .= '<li>'. $taxonomy_term->name .'</li>';
}
$output .= '</ul>';
echo $output;
}
?>
Breaking this down:
- “get_the_terms()” outputs an array if there are any terms of the declared type assigned to the post, or a boolean FALSE if not. The first thing I did was run an “if” statement to make sure any terms have even been added to this post.
- Assuming they have been added, I store the taxonomy terms in an array variable, “$taxonomy_ar”.
- Now I create a new variable to store my output, and stick the opening HTML list tag in it.
- Now I loop through the “$taxonomy_ar” array, temporarily naming each item in it “$taxonomy_term”.
- For each “$taxonomy_term” I find, I append information to the “$output” using the PHP “.=” append, and I add list item HTML tags around the term I want to display. Note that since “$taxonomy_ar” is a 3-dimensional array, each “$taxonomy_term” still has to be broken down further to pull out its name using “->name”.
- After the foreach loop, I close the HTML list tag and echo the final output.
If all went well, you should get an output of terms wrapped in an HTML list but without <a> tags. Yay!
Just to lock everything down solidly, you may also want to check if the custom taxonomy exists at all by wrapping the above in another if statement:
if(taxonomy_exists('your-custom-taxonomy-name')) {
//all that other good stuff
}
?>
And there you have it, how to display your custom taxonomy tags or categories without wrapping them in links. Hope you found it useful. :)












I’ve been going crazy trying to figure this out.
This was exactly the help I needed, worked like a charm.
Thanks for the tutorial!!
You’re welcome, glad to help. :)
Thank you so much Roxanne – its people like you that make the WordPress community so wonderful. I’ve bookmarked this one.
Thank you for the kind words! You’re very welcome.
this is great and so close to what im trying to do!
is there a way to echo the slugs instead of the names, say to use as a class???
so the output would look like:
li class=”tax-slug” (if one category is selected)
li class=”slug-one slug-two” (if 2 are selected)
your first example does exactly what I want to do, but I cant figure out how to convert the name into the slug.
your second example, ive tried a hundred different ways, and it works with the slug, until I assign two categories to a post, then it does all kind of funky things.
Hmmm I’m not sure what to do about that. I hadn’t considered what would happen when putting a post in more than one taxonomy, since my application wouldn’t run into that.
I can’t think of a solution off the top of my head, but if I do I’ll be sure to post it. Likewise, I hope you’ll write again if you find one!
Thanks for commenting!
I did finally get it working. I used the code here. Its so similar to your code it cant even tell you what made the difference, but its finally working after hours and hours of messing with code.
Thanks so much for your great post!
I wonder if it’s because they threw get_term() in the mix. At any rate, I’m glad you found a solution! Thanks for posting it. :)
Worked fantastic! Great clear example! I appreciate you posting!
This is a very useful article, but what about if I want the list on post listing page and not on single post page.
You mean like as an archive page? That’s a whole other can of worms. I had to write a custom loop to do that using ‘post_type=page’ and ‘page-categories=your-custom-taxonomy-name’.
Yes, I have tried a lot and also searched on web, but no success.
I figured out the solution with the help of this article:
http://stackoverflow.com/questions/4642624/how-to-list-all-items-in-custom-taxonomy-with-a-permalink-in-wordpress
Thanks
Great! Thanks for posting the link!
Thank U so much for posting your experience on this topic, this didn’t just solve one problem I have been struggling with, but two! So double thanks for this and thanks for explaining it so well! :)
Hi, great tut, one question how do i get the id of the taxonomy.
$taxonomy_term->ID;
doesnt seem to be working.
Thanks
echo $taxonomy_term->term_id;
Thats how… found on…
http://wordpress.org/support/topic/how-to-echo-custom-taxonomy-id
Miss Roxanne… All i can say… is… THANKYOU! Literally pulling my hair out over this one. Very Very good write up! Appreciate the sharing of this information! :)
You’re welcome! Glad it helped. :)
I see you have code to check for if the taxonomy exists, but what if the taxonomy is empty (having trouble finding that code)?
For example, I’m using a custom taxonomy for displaying guest authors. If a post doesn’t have a guest author, I’d like to display the regular author code instead.
Ok… this wasn’t so hard! Using the example from your Method #1, if you wrap the ‘echo strip_tags …;’ with ‘if ( ! empty( $terms_as_text ) ) { … }’, that seems to do the trick.
Thanks for a great post!
Thank you! It worked very well!
Marie´s last [type] ..It is hard work, this designing and building websites, but it is very rewarding & a lot of fun!
Just used this to perfection. Thanks!
Kyle´s last [type] ..Digital Learning and Badges