Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

Tags:

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 } ?>    
like image 934
Zach Smith Avatar asked Jan 06 '11 17:01

Zach Smith


People also ask

How do I limit title length in WordPress?

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.

How do I trim a post title in WordPress?

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.

How do I limit contents in WordPress posts?

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..


1 Answers

The mb_strimwidth function does exactly that.

echo mb_strimwidth(get_the_title(), 0, 50, '...'); 
like image 96
Saul Avatar answered Sep 28 '22 18:09

Saul