Blog > Tutorials > WordPress Tutorials > WordPress Title Trimming Tutorial

WordPress Title Trimming Tutorial

02.06.2010 Bookmark and Share

This tutorial will show you how to trim title lengths for long post or page titles, which can be helpful for keeping the layout of your website consistent. My example uses the WordPress get_the_title template tag, but this could easily be modified for other uses.

Start off by opening your functions.php within your WordPress theme folder:
(wp-content\themes\themename\functions.php)
If you don’t have a one, you can create a new file and call it functions.php.

Next copy the following code into your functions.php, save and then upload the file. Watch out for the PHP tags when copying the code, you’ll most likely only need them if you’re starting a blank document.

// Title Trimmer (Version 3)
// Sam Morris – 12.28.2009
// www.smor.tv

function title_trimmer($title , $maxLength)
{
$titleLength = strlen($title); // get the amount of characters in the title

// start conditional
if ($titleLength > $maxLength) // if the title length is over the max length, then we start the trimming process.
{
$trimmedTitle = substr_replace($title,”",$maxLength);
echo $trimmedTitle;
//echo $trimmedTitle . “…”; //use this instead of the line above if you prefer having a … after your trimmed titles.
}
else
{
echo $title;
}
}

WordPress has many built in functions, but the functions.php is where you can place your own custom functions. When your theme’s templates are loaded, so will the functions.php allowing you to use your own functions throughout your theme. Open the template that needs the title trimming, or if you just want to try it out you can open single.php (for posts) or page.php (for pages).

find the_title, which is what most themes will use by default. When you find it, you can replace it will the following code. The title trimming function has 2 arguments, the first is what should be trimmed and the second argument is the maximum length of characters. In my example I’m trimming the WordPress title down to a maximum of 10 characters.

title_trimmer(get_the_title() , 10);

Save, upload, and then create a test page or post to see the title trimming in action.

Sam Morris WordPress Tutorials

  1. August 15th, 2010 at 18:51 | #1

    thanks for this tutorial. Was wondering how other sites to this! ARe there any plugins that do the same thing?

    • August 15th, 2010 at 19:17 | #2

      This tutorial just shows the basics, but isn't perfect for all circumstances (for example, words can be cut in half, etc). I'm sure there's a ton of plugins that can do this, however I can't think of any off the top of my head.

      Have a look on the wordpress site, <a href="http://www.wordpress.org/extend/plugins/” target=”_blank”>www.wordpress.org/extend/plugins/

  2. Nathan
    September 10th, 2010 at 18:03 | #3

    this is perfect!
    Can you recommend a way to do this type of thing for only a particular widget?

    • September 10th, 2010 at 18:31 | #4

      Hi Nathan,

      It depends on what you're trying to do, but sometimes I need to create a widget to preform some type of standard functionality (like displaying recent posts), but don't want the titles wrapping to a 2nd line. I typically use the My Custom Widgets plugin (http://wordpress.org/extend/plugins/mycustomwidge…) and create a widget that contains a WordPress loop. Then I can grab the posts I want and have the ability to wrap the title tags in the title trimmer function. The end result would be a loop going through my recent posts with trimmed down titles.

  3. Josh
    September 20th, 2010 at 07:24 | #5

    my titles are displaying twice?!?

    this is the code I copied from above:

    // Title Trimmer (Version 3)
    // Sam Morris – 12.28.2009
    // http://www.smor.tv
    function title_trimmer($title , $maxLength){
    $titleLength = strlen($title);
    // get the amount of characters in the title
    // start conditional
    if ($titleLength > $maxLength) {
    // if the title length is over the max length, then we start the trimming process.
    $trimmedTitle = substr_replace($title,"",$maxLength); echo $trimmedTitle;
    //
    echo $trimmedTitle . "…";
    //use this instead of the line above if you prefer having a … after your trimmed titles.
    } else {
    echo $title; }}

    this is the code in my page:

    <?php title_trimmer(get_the_title() , 40); ?>

    What am I doing wrong??

    great function ;)

    • September 20th, 2010 at 19:42 | #6

      //
      echo $trimmedTitle . "…";

      That part is probably what's causing the issue. Sorry for the formatting, the plugin I was using for displaying PHP code was causing my formatting to disappear. Try to copy it from above again and it should work for you.

    • Josh
      September 20th, 2010 at 22:47 | #7

      ah – I see what I did – funny tho – looks like the code portion of this page has changed since yesterday.

      • September 20th, 2010 at 23:32 | #8

        Hey Josh,

        As per your observation I changed the code so it's formatted correctly (and can be copied without error), thanks for pointing out the issue. I still need to fix up some of my other posts, but this one should be fine.

  4. ridiboomidi
    February 24th, 2011 at 04:17 | #9

    although i had problem with if ($titleLength &gt; $maxLength) but after changing it to if ($titleLength > $maxLength) it worked greate , some characters during copy/paste changed , people Bcareful for this kind of errors , Tnx dear author :)

  5. mylife
    October 31st, 2011 at 05:52 | #10

    hello,
    I am making a jobsite and using job roller theme in which I am pulling jobs from RSS feeds, when the jobs get pulled on the wordpress site, with the job title, description and tag. Here job title are long with the job location attached with the title.

    Now I have to extract only job location such as ex. "Business Analyst – Financial Job at Yoh – NY new york" from the title and make updated in the location field column. every time I have to edit post and cut paste job location such as "NY new york" in location column manually.

    I tried trimming the title it works but how can I just extract job address text from title and auto update that in location column. I want this process automated,

    please help here

    waiting…

    Thanks

  1. No trackbacks yet.