I am trying to learn how to shorten a title only if it is over 8 characters long. If it is longer than 8 characters, then echo the first 8 characters and put an ellipse after it.
Here is how I am getting the title:
<?php echo $post->post_title ?>
Any help would be greatly appreciated. This will be a great learning lesson for me so I can replicate this in the future, so again any help would be amazing.
Alternatively, if You have the
mbstring
extension is enabled, there’s also a shorter way as suggested by Gordon’s answer. If the post’s encoding is multibyte, You’d need to usembstring
anyway, otherwise characters are counted incorrectly.You can use
mb_strimwidth
If you want to truncate with respect to word boundaries, see
You should do this in plugin because if you change the theme the changes will be lost
you can try this.
So by now you no need to change max char in all the line of code.
Thanks.