I have a WordPress site with titles, and if the title has more than 50 characters I need to add an ellipsis (...
) at the end of the title and stop the title at 50 characters.
Below is the PHP I am writing but it seems to not work correctly.
<?php if (strlen("the_title()") > 50) { ?> <?php the_title(); ?> <?php } if (strlen("the_title()") < 50) { ?> <?php echo substr(get_the_title(), 0, 50); ?>... <?php } ?>
First thing you need to do is install and activate the Limit Post Titles plugin. Upon activation, simply go to Settings » Limit Post Titles to configure the plugin settings. You need to enter the character limit and select the post types where you want to enable this character limit.
Another way to truncate WordPress post titles is by adding code directly to your WordPress theme files. This method gives you more control over where your titles are shortened. For example, you might want to only cut off titles on your homepage, but display the full-length title on the blog post.
Using add_filter() method of WordPress on the_content filter hook. Using custom php function to limit content characters. Show activity on this post. just to help, if any one want to limit post length at home page .. then can use below code to do that..
The mb_strimwidth function does exactly that.
echo mb_strimwidth(get_the_title(), 0, 50, '...');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With