Truncated WordPress Post Titles with PHP

WordPress is one of the most widely used platforms for creating websites and blogs. Often, post titles can be too long, which can affect the presentation of content on different devices and themes. In this article, we will explore how to truncating post titles in WordPress using PHPa useful technique to improve the aesthetics and readability of your site.

Why truncate titles in WordPress?

Truncating titles can be beneficial for several reasons:

  • Aesthetics: A shorter title can make your design look cleaner and more organized.
  • Usability: Improves user experience, especially on mobile devices where space is limited.
  • SEO: More concise titles can be more attractive in search results.

Method for truncating titles

Title truncation can be achieved through a simple PHP code that can be added to your theme's functions.php file. Here is an example of how to do it.

Code for truncating titles

The following PHP code will allow you to truncate the titles of your posts to a specific number of characters:

function truncate_title($title) {
    $max_length = 50; // Maximum number of characters
    if (strlen($title) > $max_length) {
        $title = substr($title, 0, $max_length) . '...'; // Add ellipses.
    }
    return $title;
}
add_filter('the_title', 'truncate_title');

In this code, we define a function called truncate_title which takes the title as input and truncates it to 50 characters. If the title is longer, '...' is added at the end to indicate that it has been truncated.

Code implementation

To implement the above code, follow these steps:

  • Log in to your WordPress administration panel.
    Truncated WordPress Post Titles with PHP
    Download our web maintenance guide Free of charge!
    Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.
  • Go to Appearance > Theme Editor.
  • Select the file functions.php in the list of theme files.
  • Scroll to the end of the file and paste the code provided.
  • Save the changes.

Remember that it is always advisable to make a backup copy of your file. functions.php before making changes.

Truncation customization

The number of characters to truncate can be modified according to your needs. Simply change the value of $max_length in the code to adjust it to your preferences.

Example of truncated titles

Below are some examples of what the titles would look like before and after truncation:

Original Title Truncated Title
Top tips to improve your SEO in 2023 The best tips to improve your...
How to optimize your website for mobile devices How to optimize your website for...

Final considerations

When truncating titles, it is important to maintain a balance between conciseness and clarity. A title that does not adequately inform the content may discourage readers. Make sure the main message remains intact even after truncating.

Alternatives to manual truncation

If you prefer not to modify the code, there are plugins that can help you truncate titles automatically. Some popular options are:

  • WP Title Truncator: A simple plugin that allows you to define the maximum length of titles.
  • Title Remover: Although its main purpose is to remove titles, it also allows settings for truncation.

These plugins can be a good option if you are not comfortable editing PHP code files.