How display on a Post its taxonomy/categories
-
We have a Custom Post (‘Event’) that has a custom taxonomy/category (‘Venue’) – used for selecting all the events at a Venue.
How do we display the category on the post?
[In Admin – it is displayed in the right-hand pane. We need to display it also on the Frontend, formatted nicely]
It’s 2024 – I had expected to be able to write pseudo-code e.g. “Variable Name: $Variable(s)
“
So far all I’ve found is the need to write bespoke PHP (see example below). Is there a better/simpler way?/* see here: https://wordpress.stackexchange.com/questions/130993/list-taxonomy-terms-in-current-post-current-category/
*
* 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 );
}
add_shortcode('test_display','get_the_current_tax_terms_wpse');
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- You must be logged in to reply to this topic.