Skip to content

Instantly share code, notes, and snippets.

@thoronas
Last active August 29, 2015 14:00
Show Gist options
  • Save thoronas/11119681 to your computer and use it in GitHub Desktop.
Save thoronas/11119681 to your computer and use it in GitHub Desktop.
Get First category associated with a post
<?php
$post_categories = wp_get_post_categories( $post->ID );
// get first category id
$first_cat = get_category($post_categories[0]);
// get category term info.
$term_link = get_term_link( $first_cat->slug, $first_cat->taxonomy );
// do what you want with it.
echo "<a href='".$term_link."'>".$first_cat->name."</a>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment