Facebook really loves to play Popularity Police. If you don’t like enough of a Page’s posts, they stop showing in […]
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.
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:
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.
$output = '<ul>''<li>''</li>';
}
$output .= '</ul>'
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:
//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 blog post ..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 blog post ..Digital Learning and Badges
The first method works perfectly for me. Thank you very much for sharing this tip with us. :-)
Thanks
Is it possible to alter this code to attach a (designated) outside URL to a taxonomy?
I want to create a ‘publications’ taxa, to link to amazon lists, and I’m exploring different ways to do that.
I have a taxonomy that has top-level terms and second-level terms. E.g
Taxonomy name: Sports
Top levels terms: Tennis, Football, Athletics, Basketball
Second level terms (under Tennis): Wimbledon, French Open, US Open, Australian Open
On single.php, how could I output the terms so that they appear like this:
Tennis
indent -> Wimbledon
indent -> US Open
indent -> Australian Open
indent -> French Open
Football
indent – > and so on..
I’d like to output the terms as text as outlined in your tutorial but cannot see how I could indent just the second level terms like i’ve shown above.
Nice method to display custom taxonomies. Thanks for sharing.
Mani Viswanathan´s last blog post ..How Smart Businesses use YouTube to Increase their Customers
Thanks so much, If you don’t mind i’m going to share this.
Major THANK YOU for this!
diana´s last blog post ..365 Project
BRILLIANT…. thank you so much! I just needed to strip the HTML tags and this was a huge time and life saver!!!
Hey,
First of all thank you for a great tutorial.
I’m planning to display a list of all clients (custom taxonomy) but remove hyperlinks for only those that don’t have case studies associated to it – though still display them. I want to show the entire list, but the ones with associated post will have hyperlink tags. How can I archive this? I really appreciate your any help in this matter.
cheers,
Ali
This worked perfectly for a project I’m finishing up. Thanks!
JP´s last blog post ..crackin nuts #gangamstyle #superbowl
Thank U!! I have a question : how can i display the parent name on the top-list? I have hierarchical custom taxonomies and i need to display parent and child like this:
Parent
-Child
-Child
Parent
-Child
-Child
-Child
Thank u!
Nice code.. See my code with different approach.. (see comment luv)
Tirumal´s last blog post ..Generate array of custom taxonomies using wp_list_categories in WordPress
Dear,
I have 2 tag_ID ( aa & bb ) in pa_colour, and I only want to show aa in new custom page, anybody can help me?
Many thank you
Andy´s last blog post ..Hợp tác kinh doanh nước hoa
To get the top categories in woocommerce email templates, cart items without the link:
$terms = get_the_terms( $_product->id , ‘product_cat’);
if($terms) {
foreach( $terms as $term ) {
$term = get_term_by(“id”, $term->parent, “product_cat”);
if ($term->parent > 0) {
$term = get_term_by(“id”, $term->parent, “product_cat”);
}
$cat_obj = get_term($term->term_id, ‘product_cat’);
$cat_name = $cat_obj->name;
}
}
echo ‘(‘. $cat_name . ‘)’;
Hi Roxanne, I realize your solution work to everyone, I have faith your method is correct, but I can’t get run on my wordpress,
I have a template theme, and this template have portfolio feature, on the theme-init.php there’s the taxonoy defined like this:
register_taxonomy(‘portfoliocat’, ‘portfolio’, array(‘hierarchical’ => true, ‘label’ => ‘Portfolio Categories’, ‘singular_name’ => ‘Category’, “rewrite” => true, “query_var” => true));
I run your 2nd method whit this code
ID, ‘portfoliocat’)) {
$taxonomy_ar = get_the_terms($post->ID, ‘portfoliocat’);
$output = ”;
foreach ($taxonomy_ar as $taxonomy_term) {
$output .= ”. $taxonomy_term->name .”;
}
$output .= ”;
echo $output;
}
And don’t work for me, could you tell me what am I doing wrong?
Before I copy my code wrong, this is the code I’m using and don’t work for me.
ID, ‘portfoliocat’)) {
$taxonomy_ar = get_the_terms($post->ID, ‘portfoliocat’);
$output = ”;
foreach ($taxonomy_ar as $taxonomy_term) {
$output .= ”. $taxonomy_term->name .”;
}
$output .= ”;
echo $output;
}
?>
Hi,
Thanks for a awesome piece of code!
I want to show the name of the taxonomy, before the actual taxonomy term? Can you help me out with that?
Regards,
Mathias