Skip to content

Instantly share code, notes, and snippets.

@ngearing
Last active June 8, 2018 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngearing/8e16c643cea72a3933cf08bcd4ef4cf2 to your computer and use it in GitHub Desktop.
Save ngearing/8e16c643cea72a3933cf08bcd4ef4cf2 to your computer and use it in GitHub Desktop.
Get list of terms for a post.
<?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