Generally speaking in the WordPress world, functions prefixed with the_
will output results immediately. Functions prefixed with get_
will return the results without outputting them.
The function get_the_terms()
can be used to achieve the desired results. Here’s a complete function that’s essentially a wrapper for get_the_terms()
with a little bit of extra formatting:
/**
* Outputs a list of terms with special formatting
*
* @param $post_id string|int ID for post
* @param $taxonomy_slug string taxonomy name
* @param $separator string separator for terms
*/
function wpse251476_the_terms( $post_id, $taxonomy_slug, $separator=" " ) {
$terms = get_the_terms( $post_id, $taxonomy_slug );
$separator = sprintf( '<span class="term-sep">%1$s</span>', esc_html( $separator ) );
// Bail if there are no terms.
if ( ! $terms || is_wp_error( $terms ) ) {
return false;
}
$links = array();
// Wrap each term link in a span and give the span the class name of the term's slug.
foreach ( $terms as $term ) {
$links[] = sprintf( '<span class="%1$s"><a href="https://wordpress.stackexchange.com/questions/251476/%2$s">%3$s</a></span>',
esc_attr( $term->slug ),
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
// Output the terms.
?>
<div class="term-list <?php echo esc_attr( __( $taxonomy_slug, 'text-domain' ) ); ?>">
<?php echo implode( $separator, $links ); ?>
</div><?php
}
Usage examples based on original code:
wpse251476_the_terms( get_the_ID(), 'rneh_status', ' ' );
and
wpse251476_the_terms( get_the_ID(), 'rneh_author', ' , ' );