This is not working because the_terms()
is echo-ing the output.
It would make more sense to trim down the lengthy term names, instead of doing it directly on the HTML output of get_the_term_list()
that’s used by get_the_terms()
. That could give unvalid HTML, that could break the layout of your site.
Here’s an example for the corresponding theme file:
// Add a filter
add_filter( 'get_the_terms', 'wpse248787_get_the_terms' );
// Display terms
the_terms( get_the_ID(), 'tapahtumat' );
// Remove the filter again
remove_filter( 'get_the_terms', 'wpse248787_get_the_terms' );
where you have defined:
function wpse248787_get_the_terms( $terms )
{
$len = 10; // <-- Adjust to your needs!
// Limit term names if needed
foreach( $terms as $term )
{
if( $len > 0 && $len < mb_strlen( $term->name ) )
$term->name = mb_substr( $term->name, 0, $len - 1, 'UTF8' ) . '…';
}
return $terms;
}
in the functions.php
file in the current theme directory. You might also need to addjust the encoding argument or use e.g. get_option( 'blog_charset' )
.