I have custom post-type ‘sermon’ with custom taxonomy ‘speaker’ attached to it. I wanted to assign custom meta values to the speaker taxonomy, so I am using the taxonomy meta class from Rilwis:
http://www.deluxeblogtips.com/p/taxonomy-meta-script-for-wordpress.html
I have used the class to add a metabox with the id of ‘bio’.
According to his directions, I need to use the following code in order to output the value of the ‘bio’ meta into my template:
$meta = get_option('meta_id');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['term_id']) ? $meta['term_id'] : array();
$value = $meta['field_id'];
echo $value; // if you want to show
Here is what ended up working for me:
$taxmeta = get_option('speaker_meta');
if (empty($taxmeta)) $taxmeta = array();
if (!is_array($taxmeta)) $taxmeta = (array) $meta;
$taxmeta = isset($taxmeta['221']) ? $taxmeta['221'] : array();
$value = $taxmeta['bio'];
echo $value; // if you want to show
Question
In the above code you see the number ‘221’. That is the actual term-id of the taxonomy assigned to the post in question (hardcoded to test it out). What I don’t understand is how to populate the term_id dynamically.
I can’t query the url because this is simply being used on a custom post-type single page, so the term-id is not available there.
How would I modify that snippet to put in the proper term-id belonging to the ‘sermon’ that I am looking at?
thanks
edit
This function does return the proper term_id for me:
$terms = wp_get_post_terms($post->ID, "speaker");
foreach ($terms as $termid) {
echo $termid->term_id;
}
Using that snippet within my loop on my example page returns the value of ‘221’. If I manually put 221 in place of term_id in the function then it does pull the term meta ‘bio’ just perfectly.
Where I am still stuck
Now that I have that little snippet above which works, how do I get that to output to the term_id so the primary function will work?
$meta = isset($meta['term_id']) ? $meta['term_id'] : array();
thanks again
you can use the
get_the_terms()
function to get the terms of that post in a specific taxonomy:and now you have the term id inside
$term_id
.update
Once you have the term_id you can use it in your function like so:
You can done it with
wp_get_post_terms()
function also:it will display all terms of taxonomy for current post.
NOTE: it must be inside the loop
If you’re on the archive.php page and need the current term: