Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

WordPress has built in function "wp_trim_words()" to trim the sentences based on the number of words you provide, If you want to trim by words then this function may help you.

https://codex.wordpress.org/Function_Reference/wp_trim_words

to trim the title longer than 5 words you can do this

<?php
$title = get_the_title();
$short_title = wp_trim_words( $title, 5, '...' );
echo '<h3>'.$short_title.'</h3>';
?>

The mb_strimwidth function does exactly that.

echo mb_strimwidth(get_the_title(), 0, 50, '...');

Single Code, 100% working

PHP Function mb_strimwidth() | Wordpress Function get_the_title()

<?php 
echo mb_strimwidth( get_the_title(), 0, 100, '...' ); 
?>

Tags:

Php

Wordpress