Created
June 15, 2019 06:36
-
-
Save thetwopct/e118189022200d81dea6cc1760ffc15e to your computer and use it in GitHub Desktop.
List categories of a single post (run inside loop)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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