Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

you can use the get_the_terms() function to get the terms of that post in a specific taxonomy:

$terms = get_the_terms( $post->ID , 'speaker' ); //change speaker to whatever you call your taxonomy
//then you can use just the first term
$term_id = $terms[0]->term_id;

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:

$term_id = $termid->term_id;

$meta = isset($meta[$term_id]) ? $meta[$term_id] : array();
share|improve this answer
    
For some reason the term_id is not being picked up when on the custom post-type template file. When I apply the code to the taxonomy template then it works fine. Maybe there are some rules I am missing for pulling the term_id on other template pages? --like does it have to be within the loop or something? –  shawn Jun 14 '11 at 11:07
    
the code i posted assumes that $post->ID is set so yeah it should be inside the loop –  Bainternet Jun 14 '11 at 14:07
    
I made absolute certain to place the code within the loop, but it does not seem to return the '221' value. I did try KOt's answer above and it does return 221, although I am not sure how to assign the output to term_id so that I can use it in my function. ideas? –  shawn Jun 14 '11 at 20:58
    
@Bainternet I edited my question above to show where I am at. Can you help show me how to get term_id populated with the new snippet? I did try your snippet but it was not returning any value, even when in the loop. thanks –  shawn Jun 14 '11 at 22:45
    
i updated my answer –  Bainternet Jun 14 '11 at 22:54
show 2 more comments

You can done it with wp_get_post_terms() function also:

$terms = wp_get_post_terms($post->ID, "speaker");
foreach ($terms as $termid) {  
  echo $termid->term_id;  
} 

it will display all terms of taxonomy for current post.

NOTE: it must be inside the loop

share|improve this answer
    
This function is returning the proper value for me of '221' when used on this page. Now that part is working, how do I 'wrap' that output of 221 into term_id so that I can use it in my function above? –  shawn Jun 14 '11 at 20:30
add comment

If you're on the archive.php page and need the current term:

var_dump($wp_query->queried_object);
var_dump($wp_query->queried_object->name);
var_dump($wp_query->queried_object->term_id);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.