views:

277

answers:

1

How am I able to show a single term of a custom taxonomy in Wordpress?

I have a custom taxonomy, which will always have only one term in it, and I want to display it in a custom loop on my home page. I have tried all possible ways, with one (blargh, can't remember which one) I managed to display the term but as a link, which I don't want. I need it as regular text, since I want to display it in my own href, which will link to the place where I want to.

Any ideas anyone? All and any help very much appreciated. This issue is slowing down my development of a custom theme. :(

A: 

Get the single term object using get_term($term_ID, $taxonomy), or alternatively get_term_by('slug', $term_slug, $taxonomy).

The term object will have, among other properties;

$term->name; // the name of the term!
$term->slug;
$term->term_id;
$term->count; // number of objects using the term
TheDeadMedic
Yes, I have tried this but it's not giving me anything at all. Maybe I'm using it wrong, but <?php get_term('taxonomy_name'); ?> isn't giving me any results.
Ragnar
The first argument needs to be the ID of the single term. The second argument is the term taxonomy.
TheDeadMedic
If you just want to get the first term of a taxonomy, use `$terms = get_terms($taxonomy)`. The first term will be `$terms[0]`.
TheDeadMedic
Still not getting anything, but probably not using it right again.<?php$terms = get_terms('issue');$terms[0]?>Like this?
Ragnar
Yes, but `$terms[0]` will do nothing on it's own - how about `echo $terms[0]->name;`?
TheDeadMedic
Thanks, man! It's working alright but it's not what I'm looking for. It's showing the term by ID, but what I need is to show whatever the term is in a specific taxonomy. I always have one term in the taxonomy, but each time it's different, so I'm looking for a way to show it.
Ragnar
I'm sure what I've provided is what you're looking for - perhaps I'm misunderstanding? Just use `get_terms()` to get all the terms for any taxonomy (even if there's only one term, an array is still returned), then use `$terms[0]` to get the first item.
TheDeadMedic
Hmm, ok, thanks again, man. I will look into it, maybe there was something wrong with my loop but it was showing the same value for all of the items the loop got, very weird.
Ragnar