Skip to content

Instantly share code, notes, and snippets.

@thetwopct
Created June 15, 2019 06:36
Show Gist options
  • Save thetwopct/e118189022200d81dea6cc1760ffc15e to your computer and use it in GitHub Desktop.
Save thetwopct/e118189022200d81dea6cc1760ffc15e to your computer and use it in GitHub Desktop.
List categories of a single post (run inside loop)
$taxonomy = 'category';
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// Separator between links.
$separator = ', ';
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( array(
'title_li' => '',
'style' => 'none',
'echo' => false,
'taxonomy' => $taxonomy,
'include' => $term_ids
) );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// Display post categories.
echo $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment