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

For a WordPress title that needs a shorter label in a card, menu, or archive template, retrieve the title with get_the_title() and pass it to wp_trim_words(). Escape the returned string for the context in which you output it:

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

This limits the displayed text to approximately 12 words and adds an ellipsis only when the original is longer. Choose a different function when the requirement is a strict character or visual-width limit.

Use wp_trim_words() for a WordPress-native word limit

wp_trim_words() accepts the source text, a maximum word count, and an optional suffix. It strips HTML tags and returns a string; the default suffix is an ellipsis when truncation occurs. See the WordPress function reference.

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

Inside The Loop, omit the post ID and WordPress uses the current post:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
echo esc_html( wp_trim_words( get_the_title(), 12, '…' ) );
?>

get_the_title() retrieves a title and applies WordPress’s the_title filter. Its behavior and parameters are documented at get_the_title().

Using the_title() in The Loop

the_title() is intended for display and echoes by default. Request the string with its third argument, then trim and escape it yourself:

<?php
$raw_title = the_title( '', '', false );
echo esc_html( wp_trim_words( $raw_title, 12, '…' ) );
?>

WordPress documents the_title() output as unescaped, so the template tag is not an escaping step. Refer to the_title() for its arguments and return behavior.

Escape the trimmed value for its output context

For ordinary visible text, use esc_html(), as in the examples above. If the title is inserted into an HTML attribute, use an attribute-specific escaper instead:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
$title = wp_trim_words( get_the_title( $post_id ), 12, '…' );
echo '<a href="' . esc_url( get_permalink( $post_id ) ) . '" aria-label="' . esc_attr( $title ) . '">';
echo esc_html( $title );
echo '</a>';
?>

Keep retrieval, trimming, and escaping separate so the same title can be safely used in different contexts.

Choose a character or visual-width limit when words are not the requirement

A word limit is suitable when approximate length is enough. It does not guarantee a fixed number of characters or a fixed visual width, and the effective counting behavior can vary for languages whose words are counted by individual characters.

Display-width truncation with mb_strimwidth()

For a label that must fit a measured width, PHP’s mb_strimwidth() truncates by display width: halfwidth characters count as one unit and fullwidth characters as two. The PHP manual documents this behavior at mb_strimwidth().

<?php
$title = get_the_title( $post_id );
$short_title = mb_strimwidth( $title, 0,  forty, '…', 'UTF-8' );
echo esc_html( $short_title );
?>

Replace forty with an integer width appropriate for the component, for example 40. Ensure the PHP mbstring extension is enabled on the deployment before using this function. Do not pass a negative width; the PHP manual marks negative width as 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 substitute substr() for multilingual titles. Bytes, Unicode code points, grapheme clusters, and display columns are different units; select the unit your design actually constrains.

Apply the trim only where the shortened title is needed

Putting the call directly in a card or archive template limits the change to that component:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <h2 class="card-title">
    <a href="<?php echo esc_url( get_permalink() ); ?>">
      <?php echo esc_html( wp_trim_words( get_the_title(), 10, '…' ) ); ?>
    </a>
  </h2>
<?php endwhile; endif; ?>

A global the_title filter can alter many title appearances, including places added by themes and plugins. Use one only when the shortened value is genuinely required across those contexts, and constrain it by post type and execution context:

<?php
add_filter( 'the_title', function ( $title, $post_id ) {
    if ( is_admin() || ! is_main_query() || get_post_type( $post_id ) !== 'post' ) {
        return $title;
    }

    return wp_trim_words( $title, 12, '…' );
}, 10, 2 );
?>

Test such a filter with the active theme and plugins because they may also filter titles. Since get_the_title() applies the the_title filter, a filter can affect code that retrieves titles as well as code that displays them.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not confuse a visible post title with the browser document title

The wp_title hook concerns the HTML document title, not an individual post heading, archive card, or navigation label. Its purpose is described in the wp_title hook reference.

If the browser tab title is the target, inspect the active theme’s document-title implementation and the wp_get_document_title() function instead: wp_get_document_title(). Use post-title APIs for visible content and document-title APIs for metadata shown by the browser.

Pick the limit from the component, not from a universal WordPress rule

WordPress’s references do not specify one supported truncation number. Set the limit from the available layout space and verify it with representative content.

  • Cards and grids: start with a word limit when a little variation in line length is acceptable.
  • Fixed-width labels: use display-width truncation and confirm the font, width, and suffix together.
  • Multilingual sites: test languages that use long compounds, scripts without spaces, and fullwidth characters.
  • Responsive layouts: check narrow and wide breakpoints; a value that fits desktop may still wrap on mobile.
  • Accessibility: keep the link’s destination and surrounding context clear; consider an untrimmed accessible label when the shortened visual text could be ambiguous.

After changing the code, inspect archive pages, search results, widgets, menus, single-post headings, and any custom post types that use the same template. This catches unintended filter scope and titles that behave differently from your sample data.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

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.