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. :)