WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
  • Deals
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Themes» How to Truncate WordPress Post Titles with PHP

How to Truncate WordPress Post Titles with PHP

Last updated on June 22nd, 2012 by Editorial Staff
15 Shares
Share
Tweet
Share
Special WordPress Hosting offer for WPBeginner Readers
How to Truncate WordPress Post Titles with PHP

While working on our client’s site, the design’s width would not allow us to keep the long titles to be displayed on the homepage. After searching the web for options, we came across several plugins that would truncate WordPress titles, but they would do it sitewide whereas we only wanted it on the homepage. In this article, we will share with you how you can truncate WordPress Post Titles with PHP.

First open your index.php or the file location where you want to truncate the title. Then paste the following code to replace your the_title tag.

<a href="<?php the_permalink() ?>">
<?php
$thetitle = $post->post_title; /* or you can use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 25;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>
</a>

Make sure you edit the $thelength variable from 25 to the character count of your need. You would have to estimate the count for your theme design. The code adds the length variable and then use the conditional tag to see if the title length matches our desired length. If it is longer, then the code adds ‘…’ in front. Most of the time, you will use this only on specific areas where the width is fixed in the theme.

*This is a good code to have as a theme designer*.

Source: Codezroz

15 Shares
Share
Tweet
Share
Popular on WPBeginner Right Now!
  • Step by Step Guide: How to Start a Podcast with WordPress

    How to Start Your Own Podcast (Step by Step)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • Why Build Your Email List Today

    Revealed: Why Building Your Email List is so Important Today!

  • Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

The Ultimate WordPress Toolkit

21 Comments

Leave a Reply
  1. Abrar says:
    Mar 22, 2017 at 3:42 pm

    Dear Staff,

    I need your help as I want Truncate WordPress Post Titles with PHP script for the thumbnails and post sliders only.

    Can you please help me to do this. I tried to use provided script in wordpress index.php but it is not working then I added this script in theme index.php but the result is remained the same.

    I will be thankful if you please provide me full script to get the desired results.

    my website which is under construction is anews.pk

    Regards.

    Reply
  2. Achintha says:
    May 8, 2013 at 7:45 am

    Hey There is a wordpress function for limit words. Better to use that.

    http://codex.wordpress.org/Function_Reference/wp_trim_words

    Reply
  3. dustinporchia says:
    Sep 29, 2011 at 3:38 am

    This is golden!….Thanks wpbeginner!

    Reply
  4. adm_mnz says:
    Jul 1, 2011 at 1:09 pm

    If you use mb_substr there is a parameter for encoding.

    http://php.net/manual/en/function.mb-substr.php

    Reply
  5. Junaid says:
    Jan 9, 2011 at 2:39 pm

    Sweet! was just looking for a clients project

    Reply
  6. Marco says:
    Sep 13, 2010 at 12:34 pm

    Does anybody know how the link title of previous_post_link(); could be truncated?

    thanks

    Reply
  7. Brow says:
    Sep 8, 2010 at 9:16 am

    Thanks this worked perfectly! I didn’t want to end up using a plugin just to do this and was happy your code cut down the titles properly.

    Thanks again!

    Reply
  8. Joey Figaro says:
    Aug 24, 2010 at 11:57 pm

    Hey there – thanks for writing this up! I happened to stumble upon another example of how to achieve this and it seemed a lot more simple, so I will share it with you and see what you think.

    functions.php:

    function new_excerpt_length($length) {
    return 100;
    }
    add_filter(‘excerpt_length’, ‘new_excerpt_length’);

    Reply
    • Editorial Staff says:
      Aug 25, 2010 at 4:06 am

      These are two entirely different concepts…. The one you recommend is for post excerpts whereas the one we are talking about is for Post Titles.

      Reply
      • Joey Figaro says:
        Sep 29, 2010 at 11:24 am

        Wow, that’s embarrassing. :)

        Reply
  9. Lena says:
    Aug 20, 2010 at 12:47 pm

    Hi!
    This doesn’t work if you use other languages than english. My swedish titel looks awful because the code doesn’t translate å ä and ö comparing to the default code. Any suggestions of what I have to do? This is a good trick and i want to use it.

    Kindly Lillan

    Reply
    • Editorial Staff says:
      Aug 20, 2010 at 2:24 pm

      Hmm… that does sound like a serious issue. Wondering if you can specify the language via PHP, so it counts characters in that instead of english.

      Reply
      • Alex says:
        Nov 16, 2012 at 6:46 pm

        You might wanna try to specify a different charset, check Latin1 or utf8 i think they contain those chars as well.. I had some similar issues recently since my first language is german :)

        Reply
        • Editorial Staff says:
          Nov 17, 2012 at 5:10 am

          Thank you for helping out Alex :)

  10. Ben Kulbertis says:
    Aug 17, 2010 at 7:56 pm

    Thanks for the Trackback!

    Reply
    • Editorial Staff says:
      Aug 17, 2010 at 11:38 pm

      We appreciate your work for the community. Thanks for the nice snippet :)

      Reply
  11. Navjot Singh says:
    Aug 16, 2010 at 11:28 pm

    One Suggestion, this type of code should be included in functions.php and not index.php. You can use conditional tags to restrict the code to any page you want whether its the homepage or any other page where you want.

    Reply
    • Editorial Staff says:
      Aug 17, 2010 at 2:41 am

      This is just for specific areas… But yes, it can be customized and placed in functions.php

      Reply
  12. Thomas Scholz says:
    Aug 16, 2010 at 5:41 am

    Don’t use strlen(). Use mb_strlen() or strlen(utf8_decode($str)) or you risk to truncate the string inside of a multi-byte character. The same applies to mb_substr().

    Oh, and an ellipsis is one character: … :)

    Reply
    • snipsley says:
      Sep 20, 2011 at 10:27 am

      Thanks!! mb_strlen() a mb_substr solved my encoding problem. I’ve been looking for this for hours!

      Reply
    • Lena Backstedt says:
      May 14, 2012 at 5:51 am

      BIG tnx!
       
      mb_strlen() also seems to work for the swedish language (so far I can see)

      Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 600,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
MonsterInsights
MonsterInsights
Google Analytics made easy for WordPress. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 25 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2018 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2018)
    • Which is the Best WordPress Slider? Performance + Quality Compared
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • 5 Best VPN Services for WordPress Users (Compared)
    • HostGator Review - An Honest Look at Speed & Uptime (2018)
    • SiteGround Reviews from 1032 Users & Our Experts (2018)
    • Bluehost Review from Real Users + Performance Stats (2018)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Start a Podcast with WordPress (Step by Step)
    • How to Choose the Best Domain Name (8 Tips and Tools)
    • How to Setup a Professional Email Address with Google Apps and Gmail
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2018 – Step by Step Guide
Deals & Coupons (view all)
MonsterInsights Coupon Code
MonsterInsights Coupon
Get 10% off MonsterInsights, the best Google Analytics plugin for WordPress.
De:comments for WordPress
De:Comments Coupon
Get 30% off on DeComments WordPress plugin. Exclusive offer for WPBeginner users only.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • List25
  • Awesome Motive
  •  

Copyright © 2009 - 2018 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

WordPress hosting by HostGator | WordPress CDN by MaxCDN | WordPress Security by Sucuri.