Archived topic
How do I display a Custom Taxonomy on my Single Posts
8 replies · Started by Nates on March 12, 2020
Hi
I created a taxonomy called Topics, using Custom Post Type UI. On my single posts, I would to keep displaying the category/tag but also include the topics taxonomy.
How do I achieve this please?
Regards
Hi there,
Try this:
add_action( 'generate_post_meta_items', function( $item ) {
$term_list = get_the_term_list( get_the_ID(), 'topic', '', ', ' );
if ( 'topics' === $item && $term_list ) {
printf( '<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ', // WPCS: XSS ok, sanitization ok.
esc_html_x( 'Topics', 'Used before category names.', 'generatepress' ),
$term_list,
apply_filters( 'generate_inside_post_meta_item_output', '', 'topics' )
);
}
} );
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
return array(
'categories',
'tags',
'topics',
'comments-link',
'post-navigation',
);
} );
Thanks, but it did not work. The CTX slug is 'topic'
add_action( 'generate_post_meta_items', function( $item )
{
$term_list = get_the_term_list( get_the_ID(), 'topic', '', ', ' );
if ( 'topic' === $item && $term_list ) {
printf( '<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ', // WPCS: XSS ok, sanitization ok.
esc_html_x( 'Topics', 'Used before category names.', 'generatepress' ),
$categories_list,
apply_filters( 'generate_inside_post_meta_item_output', '', 'topic' )
);
}
} );
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
return array(
'categories',
'tags',
'topic',
'comments-link',
);
} );
$term_list = the_terms( get_the_ID(), 'topic', '', ', ' );
I made this change (the_terms instead of the_terms_list) and it now displays my topic x3 times on the single post... ?
Can you try the updated code above?: https://generatepress.com/forums/topic/how-do-i-display-a-custom-taxonomy-on-my-single-posts/#post-1194051
I did originally and never worked, I had to change topics to topic, then I tried the other stuff I mentioned to TRY and get it to work..
Did you try it again? I updated it after your last reply.
Thanks! It iss working.. How do I add an icon to the front though?
You can do this:
add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
if ( 'topic' === $item ) {
return 'YOUR ICON ELEMENT HERE';
}
return $output;
}, 10, 2 );