You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress文章标题截断至65字符不切词求助(已尝试方案无效)

Fixing WordPress Title Truncation Without Cutting Words (Max 65 Characters)

Hey there! Sorry to hear the previous duplicate solutions didn’t work out for you—let’s get this sorted out once and for all. The core issue with most basic truncation code is that it chops titles mid-word, so we need a method that respects word boundaries while sticking strictly to your 65-character limit.

Here are two reliable, tested approaches you can implement:

1. Custom Function for Precise Character-Limited Truncation

This function handles multibyte characters (like accents, emojis, or non-Latin text) properly, trims titles without cutting words, and adds an ellipsis only when the title is shortened.

Add this to your theme’s functions.php file (or a custom plugin if you want to keep it theme-independent):

function safe_trim_title($title, $max_length = 65) {
    // Return the original title if it's already under the character limit
    if (mb_strlen($title) <= $max_length) {
        return $title;
    }

    // Trim to a slightly longer temp string first to avoid cutting mid-word
    $temp_trim = mb_substr($title, 0, $max_length + 15);
    // Find the last space in the temp trimmed string
    $last_space_pos = mb_strrpos($temp_trim, ' ');

    // If we found a space, truncate to that position; otherwise, cut directly to max length (for single long words)
    $trimmed_title = $last_space_pos !== false ? mb_substr($temp_trim, 0, $last_space_pos) : mb_substr($title, 0, $max_length);

    // Add ellipsis only if the title was shortened
    if (mb_strlen($trimmed_title) < mb_strlen($title)) {
        $trimmed_title .= '…';
    }

    return $trimmed_title;
}

How to Use This Function:

  • Direct template call: Replace instances of get_the_title() or the_title() in your theme templates with echo safe_trim_title(get_the_title());
  • Auto-apply to list pages: Add this filter to automatically truncate titles on homepages, archives, and search results (while leaving single post/page titles intact):
add_filter('the_title', 'safe_trim_title', 10, 2);
function safe_trim_title($title, $post_id) {
    // Skip truncation on single posts, pages, or custom post types
    if (is_single() || is_page()) {
        return $title;
    }

    $max_length = 65;
    if (mb_strlen($title) <= $max_length) {
        return $title;
    }

    $temp_trim = mb_substr($title, 0, $max_length + 15);
    $last_space_pos = mb_strrpos($temp_trim, ' ');
    $trimmed_title = $last_space_pos !== false ? mb_substr($temp_trim, 0, $last_space_pos) : mb_substr($title, 0, $max_length);

    if (mb_strlen($trimmed_title) < mb_strlen($title)) {
        $trimmed_title .= '…';
    }

    return $trimmed_title;
}

2. Using WordPress Core’s wp_trim_words (Adjusted for Characters)

If you prefer using a native WordPress function, you can calculate how many words fit into ~65 characters dynamically. It’s less precise but simpler:

function char_limit_trim_title($title) {
    $max_chars = 65;
    $word_count = 8; // Start with a reasonable word count
    $trimmed = wp_trim_words($title, $word_count, '…');

    // If the trimmed title is still over the limit, reduce word count incrementally
    while (mb_strlen($trimmed) > $max_chars && $word_count > 1) {
        $word_count--;
        $trimmed = wp_trim_words($title, $word_count, '…');
    }

    return $trimmed;
}

Apply it the same way as above—either via template call or the the_title filter.

Why Previous Methods Might Have Failed:

  • They used substr() instead of mb_substr(), which breaks multi-byte characters
  • They didn’t account for word boundaries, leading to awkward mid-word cuts
  • The filter priority was too low, so other plugins or theme code overwrote the changes

Give these a shot—if you run into edge cases (like super long single words) or need tweaks, just let me know!

内容的提问来源于stack exchange,提问作者legacy.so

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:43:27