24

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 } ?>   
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Zach Smith
  • 5,490
  • 26
  • 84
  • 139
  • What does it not do that you expect it to? Also, remove the quotes around all uses of `the_title()`. You want the return value, not that string. – Glen Solsberry Jan 06 '11 at 17:31

9 Answers9

95

The mb_strimwidth function does exactly that.

echo mb_strimwidth(get_the_title(), 0, 50, '...');
Saul
  • 17,973
  • 8
  • 64
  • 88
  • 1
    +1 , why should we reinvent the wheel when we allready have one ? – Poelinca Dorin Jan 06 '11 at 17:37
  • how do i put the wordpress title into the variable? – Zach Smith Jan 06 '11 at 17:38
  • @Saul, nice answer! I like it. – Brad Jan 06 '11 at 17:42
  • @HollerTrain `$title = get_the_title();` – NickAldwin Jan 06 '11 at 17:42
  • @Saul Wow, I had no idea this existed. Thanks, now I know! :) – NickAldwin Jan 06 '11 at 17:42
  • absolutely perfect with much less coding. I REALLY appreciate it as i have learned something just now. – Zach Smith Jan 06 '11 at 17:48
  • Thanks) Pretty easy and elegant solution) I just wanted to add that if you want to get the title of the post for trimming with the function the_title() you need to use it with the params - iw worked for me: – Paul Basenko Aug 23 '17 at 14:49
  • @PaulBasenko you should really use `get_the_title()` instead of using the third argument `false` on `the_title()` function – MacK Jan 03 '18 at 11:10
  • The `mb_strimwidth` is deprecated and no longer working in PHP 7.2, so [some sort of changes are needed](http://onezeronull.com/2018/03/13/replacement-for-mb_strimwidth-which-is-deprecated-in-php-7-2/) as this answer is no longer valid for newest versions of PHP. – trejder Mar 13 '18 at 19:18
  • @Saul Really, really sorry for that, but my comment was way to quick and confused you. The `mb_strimwidth` is _not_ deprecated in PHP 7.2. It is only part of `php-mbstring` package which may be (as in my case) disabled by default in newer version of PHP 7.2 (in hosting configuration). For details, please review last paragraphs of [updated blog entry](http://onezeronull.com/2018/03/13/replacement-for-mb_strimwidth-which-is-part-of-php-mbstring-package/) and consider rolling back changes you made to your otherwise perfect answer after my incorrect suggestions. Really sorry once again. – trejder Mar 15 '18 at 09:09
  • @trejder -- Lol, and I took your word for it :-) But I guess that's what happens when you have written an answer on a technology you no longer work with. Cheers! – Saul Mar 15 '18 at 11:12
  • Clean and straightforward approach. +1 Cheers!! – AndrewL64 May 01 '18 at 22:44
  • Nice and clean! Still on going on 2023 . Thank you – Nuno Sarmento Jun 19 '23 at 20:15
9

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>';
?>
Aamer Shahzad
  • 2,617
  • 1
  • 27
  • 25
  • 2
    This is exactly what I needed. Thank you – sk03 Nov 30 '18 at 14:36
  • 1
    The OP asks for limiting by characters, but I think this answer is very helpful and just what I wanted – JDandChips Oct 05 '20 at 12:04
  • is it possible to do this only in mobile view? if so? what is the code look like? – Marvin Acosta Mar 04 '22 at 04:29
  • @MarvinAcosta you can achieve that with css text-overflow: ellipsis and line-clamp (for multiline texts) which you can combine with media queries e.g. Check this out: https://stackoverflow.com/questions/3922739/limit-text-length-to-n-lines-using-css – Rych Jan 12 '23 at 17:33
5

Single Code, 100% working

PHP Function mb_strimwidth() | Wordpress Function get_the_title()

<?php 
echo mb_strimwidth( get_the_title(), 0, 100, '...' ); 
?>
Subroto Biswas
  • 553
  • 7
  • 5
2

Add this to your "functions.php" file in your theme folder....

function the_title_excerpt($before = '', $after = '', $echo = true, $length = false) 
  {
    $title = get_the_title();

    if ( $length && is_numeric($length) ) {
        $title = substr( $title, 0, $length );
    }

    if ( strlen($title)> 0 ) {
        $title = apply_filters('the_title_excerpt', $before . $title . $after, $before, $after);
        if ( $echo )
            echo $title;
        else
            return $title;
    }
}

then call the title like as follows

<?php the_title_excerpt('', '...', true, '50'); ?>
Ali
  • 1,326
  • 1
  • 17
  • 38
1

You're checking the length of the string "the_title()". Remove the quotes, and it will probably work (I'm not 100% sure of the difference between the_title() and get_the_title(), as I haven't used Wordpress in a while -- you might have to switch that around too):

<?php if (strlen(the_title()) > 50) { ?>
                <?php the_title(); ?>
            <?php } if (strlen(the_title()) < 50) { ?>
                <?php echo substr(get_the_title(), 0, 50); ?>...
            <?php } ?>   

or maybe

<?php if (strlen(get_the_title()) > 50) { ?>
                <?php the_title(); ?>
            <?php } if (strlen(get_the_title()) < 50) { ?>
                <?php echo substr(get_the_title(), 0, 50); ?>...
            <?php } ?>   
NickAldwin
  • 11,584
  • 12
  • 52
  • 67
  • the second one seemed to work. do you know of better code that could be used instead of this elementary `if if` i am using? – Zach Smith Jan 06 '11 at 17:43
  • @HollerTrain Yep...use Saul's answer :). I posted a response showing you how to use the variable. – NickAldwin Jan 06 '11 at 17:45
1
<?php 
$title  = the_title('','',false);
if(strlen($title) > 60):
    echo trim(substr($title, 0, 65)).'...';
else:
    echo $title;
endif;
?>
Johan Wallberg
  • 107
  • 4
  • 11
0

Take the_title() out of quotes when using the strlen() function.

Aaron Hathaway
  • 4,280
  • 2
  • 19
  • 17
0
echo (strlen(the_title())>50) ? (substr(the_title(), 0, 50) . "...") : the_title());

This is a ternary operator. What it basically says is if the result from the_title() is more than 50 characters, then echo the first 50 characters and then the string .... Otherwise, just echo the result from the_title().

You can read more about substr here: http://php.net/manual/en/function.substr.php

You can find info on the ternary operator here: http://php.net/manual/en/language.operators.comparison.php

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Well, where is the title? I don't have your wordpress installation in front of me. I would have assumed that the function `the_title()` would return a string containing the title. Does it not? – Brad Jan 06 '11 at 17:41
  • @Brad I think `the_title()` formats the title, whereas `get_the_title()` returns the title string. But I'm no Wordpress expert. – NickAldwin Jan 06 '11 at 17:44
  • Ah well in that case HollerTrain, just replace everything up there that says `the_title()` with `get_the_title()`, but you should really follow Saul's answer. It's better. – Brad Jan 06 '11 at 17:45
  • if you are using strlen you have to use `get_the_title` i believe. anyway, i put your code in the index.php and i get a white screen so seems the code is breaking the site. ty for your help though. – Zach Smith Jan 06 '11 at 17:46
  • i replaced everything with `get_the_title()` and still not working – Zach Smith Jan 06 '11 at 17:46
0

use 'strlen'

eg: 

  <?php echo ((strlen(get_the_title())>50) ? (substr(get_the_title(), 0, 50) . "...") : get_the_title())?>
Vishnu Gopinath
  • 55
  • 1
  • 10