Trying to output a post’s terms (taxonomy) as text, not URLs
-
I’m looking for something similar to get_the_term_list but in a way that doesn’t output the results as URLs.
I would just like the actual term names for a specific taxonomy that are associated with the post.
I’m all ears!
-
Hi there, thx for the quick answer.
That’s definitely the right direction. I tried using the following:
<?php echo get_the_terms($post->ID, ‘type’); ?>
but all that is echoed is the word ‘array’. How do I print the results of this array? I’m new to PHP and even newer to arrays. ??
David
I think valid values are..
category
post_tag
and
link_category
I don’t think
type
will give you any results…NOTE: to print an array, use
print_r($yourarray);
that’s what print_r is for… ??According to the first link, where I used the word ‘terms’ is in reference to my taxonomy title.
I now tried:
$terms = get_the_terms($post->ID, 'cuisine_type'); print_r($terms);
Now I get the output:
Array ( [7] => stdClass Object ( [term_id] => 7 [name] => Mexican [slug] => mexican [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => cuisine_type [description] => This is the Mexican food section [parent] => 0 [count] => 2 [object_id] => 6 ) )
The ‘type’ in this case is ‘Mexican’. That’s what I’d like to print.
Can anyone tell me if I’m on the right track? How can I echo just the ‘Mexican’ term in the array? I’ve been trying to accomplish this for hours with no luck. Any help is appreciated!
When you’re talking about terms, are you actually dealing with categories?…
I say this because i’m still fairly new (this year anyway) to WordPress and whenever i see the mention of “terms” it just confuses me a little because i’m use to referring to categories..
Perhaps if you explain what it is you’re attempting to do someone can suggest an easier approach…
There’s bundles of category functions, who knows there may be one suited to what you want to do…
Or am i losing the plot, and totally off the mark, lol…
Thanks for the reply. Actually I’m talking about a new feature in 2.8 called taxonomy. It works a bit different than tags and categories.
https://codex.www.ads-software.com/Function_Reference/get_the_term_list
This function works like a charm to list my taxonomy terms as links but I’d like to use those same terms as normal text within the WordPress post templates as well.
If you’d like to learn about it, check the functions here: https://codex.www.ads-software.com/Function_Reference (find ‘taxonomy’).
The article that got me into taxonomy is this one, check it out as well if you’re interested: https://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database
Thanks for trying to help. If you can help more, please do, if not, enjoy the reading!
I’ll have a good read when i can spare more time then to have a brief skim … in the mean time perhaps you might be fortunate enough to catch Justin, he does frequent the forum..
Or perhaps whooami can advise further, because i’d only be guessing or making assumptions at this point, and that may not be of much help to you..
Generally when you want to grab data from an array you’d use some form of loop, to loop over each item in the array unless you’re selecting a particular item from the given array…
If i knew something more definitive about taxonomies i’d give you a more useful answer to work with… ??
I’ll be watching the thread in any case…. ??
Try this:
<?php echo $terms['name']; ?>
That specifies exactly which index you want from the array.I was trying to output all of the terms from a taxonomy and ended up with something like this. I made a few changes to suit your situation. Though I haven’t tried it, I’d hope it works.
<?php //Your code: $terms = get_the_terms($post->ID, 'cuisine_type'); print_r($terms); //my code foreach ($terms as $taxindex => $taxitem) { if ($taxindex=='name') { ?> <span><?php echo $taxitem; ?></span> <? } //end if } // end foreach ?>
In theory, your code would give you the array you posted earlier. Then my code would loop through the array and for each index that matches ‘name’ (which is where “Mexican” would be stored) will echo the value.
Hope that helps.
Adz, the result is an array, each item’s value is an object..
Something like this should work though…
<?php // Get terms for post $terms = get_the_terms( $post->ID , 'cuisine_type' ); // Loop over each item since it's an array foreach( $terms as $term ) { // Print the name method from $term which is an OBJECT print $term->name . '<br />'; // Get rid of the other data stored in the object, since it's not needed unset($term); } ?>
Very basic example, but referencing data in an object is a little different to that in array.
You can test the above by changing cuisine_type to post_tag if your posts have tags (rather then having to create custom taxonomies and terms just to test it).
Thanks for the replies! I found a pretty simple way that I hadn’t thought of… Basically using get_the_term_list and then using strip_tags to get just the terms as text.
Works like a charm. Hopefully this helps someone in the future.
Example:
$terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'taxonomyyouwanttoquery', '', ', ', '' ) ); echo $terms_as_text;
Just replace the taxonomyyouwanttoquery with the taxonomy you’d like to check against for terms associated to the post.
I looked at get_the_term_list several times, and still didn’t think to use that because it generated links, can’t believe i didn’t consider strip_tags …. I over-thought this one…
Very easy way to handle it… ??
@widecast, @31os_, @adz,
Unfortunately I couldn’t get the examples of code to work off the bat (as warned might be the case).This did work:
<?php $terms_as_text = get_the_term_list( $post->ID, 'custom taxonomy name', '', ', ', '' ) ; echo strip_tags($terms_as_text); ?>
And this works as well:
<?php $terms = get_the_terms($post->ID, 'custom taxonomy name'); print_r($terms); echo '<ul>'; foreach ($terms as $taxindex => $taxitem) { echo '<li>' . $taxitem->name . '</li>'; } echo '</ul>' ?>
My goal was to show the terms of a post from a custom taxonomy in the sidebar as an unordened list.
Thanx for sharing the code. ??
I had posted this previously, but noticed a HTML error at the last minute.
If you want to use the get_the_term_list() for an unordened list, I encountered the following in order to make it work. Otherwise the first record is left without an li tag.
<?php echo '<ul>'; $terms_of_post = get_the_term_list( $post->ID, 'custom taxonomy name', '<li>','<li>', '</li>', '' ); echo $terms_of_post; echo '</ul>'; ?>
One would expect:
<?php $terms_of_post = get_the_term_list( $post->ID, 'custom taxonomy name', '<ul>','<li>', '</li>', '</ul>' ); echo $terms_of_post; ?>
to work.
Subject to review. Can anyone confirm this? Or maybe we should clear up the Codex page on get_the_term_list().
- The topic ‘Trying to output a post’s terms (taxonomy) as text, not URLs’ is closed to new replies.