Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
PHP

How to Truncate WordPress Post Titles With PHP

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a shorter post title in a WordPress template, retrieve it with get_the_title(), trim it with wp_trim_words(), then escape it for the HTML context where it appears. This limits the displayed version without changing the stored post title.

Trim a title to a word limit in a template

Use this where the title is rendered, such as a post card or archive listing:

<?php
$title = get_the_title( $post_id );
echo esc_html( wp_trim_words( $title, 12, '…' ) );
?>

Replace $post_id with the relevant post ID and 12 with the word limit that fits your component. In The Loop, you can call get_the_title() without an argument to retrieve the current post’s title. The function applies the the_title filter, so plugins or themes that filter titles can affect the returned string. See WordPress’s get_the_title() reference.

wp_trim_words() accepts the text, a word count, and an optional suffix. It returns a string, strips tags, and uses an ellipsis by default when the text exceeds the limit. The example supplies … explicitly. WordPress notes that for languages where words are counted by individual characters, the count may operate on characters instead; the function also passes through the wp_trim_words filter.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Escape the result for its output context

The example uses esc_html() because the shortened title is printed as text in HTML. If you put it in an HTML attribute instead, escape it with the function intended for attributes. Do not assume that a title template tag makes output safe by itself: WordPress documents that the_title() output is unescaped.

the_title() is a display-oriented template tag and echoes by default. Its third argument can request the title as a string, but retrieving the title with get_the_title() is a straightforward option when you need to pass it into another function before output.

Choose the limit that matches the layout

A word count is useful when approximate length is acceptable, but it does not guarantee a fixed number of characters or a consistent visual width. There is no universally supported number of words for a WordPress title: choose a limit for the particular card, menu, or other component, then check representative titles, translations, and screen sizes.

Use display width when visual fit matters

For a width-based cap, PHP provides mb_strimwidth(). The PHP manual defines its width calculation this way: halfwidth characters count as one unit and fullwidth characters as two. Confirm that the mbstring extension is available in the environment where your code runs. Negative width is deprecated as of PHP 8.3.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not casually replace a width-aware approach with substr() for multilingual titles. Bytes, characters, grapheme clusters, and display width are different units; use one that matches the design requirement. The cited PHP reference establishes display-width truncation, not a universal character-truncation recipe for every language and encoding.

Approach What it limits Best fit Considerations
wp_trim_words() Words, with character-count behavior for some languages A component where approximate title length is sufficient WordPress-native; returns a string and accepts an optional suffix.
mb_strimwidth() Display width, counting halfwidth characters as one unit and fullwidth as two A component that needs a width-based cap Requires the mbstring extension; PHP 8.3 deprecates negative width.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Apply the change only where it belongs

Trimming at the template call site keeps the change local: the stored title and other displays remain untouched. A global the_title filter can alter titles in multiple places, so use one only when that broader behavior is intended. If you do use a filter, constrain it to the intended context and post type, return the filtered string, and check how the active theme and plugins interact with it.

Also distinguish the visible post heading from the browser tab title. The wp_title hook filters the HTML document title; it is not the API for shortening an individual visible post title. For document-title behavior, inspect the active theme’s implementation and WordPress’s wp_get_document_title() reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.