Last active
June 8, 2018 05:42
-
-
Save ngearing/8e16c643cea72a3933cf08bcd4ef4cf2 to your computer and use it in GitHub Desktop.
Get list of terms for a post.
This file contains 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
<?php | |
/** | |
* Get an array of WP_Term objects for a post. | |
* | |
* @param integer $post_id The post id. | |
* @return array | |
*/ | |
function get_post_terms( $post_id ) { | |
$terms_array = []; | |
$taxonomies = get_the_taxonomies( $post_id ); | |
foreach ( $taxonomies as $taxonomy => $name ) { | |
$terms_array = array_merge( $terms_array, get_the_terms( $post_id, $taxonomy ) ); | |
} | |
return $terms_array; | |
} | |
// Then you can format and display the list anyway your like. eg. | |
printf( | |
'Go back to - %s', | |
implode( | |
' / ', | |
array_map( function( $t ) { | |
return sprintf( | |
'<a href="%s">%s</a>', | |
get_term_link( $t->term_id, $t->taxonomy ), | |
$t->name | |
); | |
}, get_post_terms( get_the_id() ) ) | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment