Skip to content

Instantly share code, notes, and snippets.

@wpexplorer
Created November 5, 2013 18:37
Show Gist options
  • Save wpexplorer/7323824 to your computer and use it in GitHub Desktop.
Save wpexplorer/7323824 to your computer and use it in GitHub Desktop.
// Displays the first category of a given portfolio post, but show all cateegories on the single post
function wpex_portfolio_first_cat($postid=false) {
global $post;
$postid = $postid ? $postid : $post->ID;
$cats = get_the_terms( $postid, 'portfolio_category' );
$output = '';
if( $cats ) {
$cats_count = count($cats);
if ( is_singular('portfolio') ) {
$count=0;
foreach( $cats as $cat ) {
$count++;
if ( $cats_count == $count ) {
$output .= '<a href="'. get_term_link($cat->slug, 'portfolio_category') .'" title="'. $cat->name .'">'. $cat->name .'</a>';
} else {
$output .= '<a href="'. get_term_link($cat->slug, 'portfolio_category') .'" title="'. $cat->name .'">'. $cat->name .'</a>, ';
}
}
} else {
$count=0;
foreach( $cats as $cat ) {
$count++;
if ( $count == 1 ) {
$output .= '<a href="'. get_term_link($cat->slug, 'portfolio_category') .'" title="'. $cat->name .'">'. $cat->name .'</a>';
}
}
}
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment