i need to display taxonomy terms in a custom post and also in archive / category page of that post type. terms must be from current post or current category https://stackoverflow.com/questions/15502811/display-current-post-custom-taxonomy-in-wordpress code in this thread works for only single post, can i make it work in archive / category page too pls.
1 Answer
You can try this:
/**
* List the taxonomies and terms for a given post
*
* @param int $post_id
* @return string
*/
function get_the_current_tax_terms_wpse( $post_id )
{
// get taxonomies for the current post type
$taxonomies = get_object_taxonomies( get_post_type( $post_id ) );
$html = "";
foreach ( (array) $taxonomies as $taxonomy)
{
// get the terms related to the post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) )
{
$li = '';
foreach ( $terms as $term )
$li .= sprintf( '<li><a href="%s">%s</a></li>',
get_term_link( $term->slug, $taxonomy ),
$term->name );
if( ! empty ( $li ) )
$html .= sprintf( '<li><ul><li>%s:</li>%s</ul></li>',
$taxonomy,
$li );
}
}
return sprintf( '<ul>%s</ul>', $html );
}
where you can call it with:
echo get_the_current_tax_terms_wpse( get_the_ID() );
from your template.