Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

found close stuff but none that exactly answers my question.. so..

How do i get the only the first taxomony (category) of a custom post type...
i can get all - no problem.. this what i am using to grab all of them

<?php foreach ($terms as $term) {echo '<a href="'.get_term_link($term->slug, 'sitecat').'">'.$term->name.'</a>,';} ?> >> <a href="<?php the_permalink(); ?>"><?php the_title('', ''); ?></a></h2></span>

Would appreciate an answer using my code but any help is most welcomed

Thanks, Sagive.

share|improve this question

1 Answer

up vote 4 down vote accepted

I'm not sure what you mean by 'first' taxonomy... but,

$terms = get_the_terms( $post->ID, 'mytaxonomy' );

returns an array of taxonomy term objects, so

$term = array_pop($terms);

Would give you the first term in the array. And then:

echo '<a href="'.get_term_link($term->slug, 'mytaxonomy').'">'.$term->name.'</a>,'

(You may want to include some if statements, in case an empty array or error is returned (see is_wp_error)

share|improve this answer
just tried this.. getting an error: Catchable fatal error: Object of class WP_Error could not be converted to string in /home/content/14/6469114/html/wp-content/themes/sagive/single-website.php on line 40 – Sagive SEO Mar 10 '12 at 3:10
on line 40: <?php $terms = get_the_terms( $post->ID, 'sitecat' ); array_pop($terms); echo '<a href="'.get_term_link($term->slug, 'sitecat').'">'.$term->name.'</a>'; ?> – Sagive SEO Mar 10 '12 at 3:11
ok.. the error was on my side - not your answer ;) thanks a lot mate – Sagive SEO Mar 16 '12 at 20:34

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.