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 need to display the first custom taxonomy attached to the post on post single page. The custom taxonomy is similar to category attached to the posts. I need to display the first custom taxonomy with link to be displayed.

My initial thoughts is to use get_categories but theres now way to define a specific taxonomy?

I get something similar for category by passing this kind of query:

<?php $category = get_the_category();
if ($category) {
    echo '<a href="' . get_category_link( $category[0]->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ';
}
?>

But something similar for custom taxonomy is not working.

Any help is much appreicated, Thanks

share|improve this question
add comment

1 Answer

You can get every Taxonomy by

$categories = get_the_terms( get_the_id(), $taxonomy-slug );

if ( is_array( $categories ) ) {

    foreach ( $categories as $category ) {
        echo '<a href="' . get_term_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }

}

Just fill in the slug of your custom taxanomy, and you are good to go.

If you just want to output the first Name, make a break after the echo.

get_the_terms() returns an Array of Taxonomyobjects, and you can easily output the first one.

share|improve this answer
    
Didn't get an idea, can you elaborate with some more detail? thanks –  Krunal Dec 27 '12 at 7:47
    
There you go, @Krunal :-) –  fischi Dec 27 '12 at 9:04
    
Hi, tried but this doesn't worked.. –  Krunal Dec 27 '12 at 10:05
    
can you post your code? –  fischi Dec 27 '12 at 10:06
    
Just the code you provided, and passing only the taxonomy-slug of my custom taxonomy –  Krunal Dec 27 '12 at 10:08
show 7 more comments

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.