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

WordPress文章标题限制65字符且不截断单词问题求助

WordPress 长标题SEO优化:批量截断至65字符方案

Hey man, 我太懂你这种老站SEO优化的痛点了——1万篇文章里6千多标题超65字符,不仅搜索结果里显示不全,搜索引擎的权重分配也会受影响。之前的代码没解决问题?大概率是没考虑多字节字符、生硬截断单词或者没区分前后台显示,给你几个针对性的靠谱方案:

一、前端显示截断(不碰数据库,快速生效)

如果不想修改数据库里的原始标题,只是在前端(搜索结果、文章列表)展示截断后的版本,这个函数更智能——不会把单词劈成两半,还支持中文等多字节字符:

function custom_truncate_title($title, $id = null) {
    $max_length = 65;
    
    // 只在前端显示时截断,后台编辑保留完整标题
    if (!is_admin()) {
        $clean_title = strip_tags($title); // 先清掉HTML标签,避免截断到标签中间
        
        if (mb_strlen($clean_title) > $max_length) {
            // 先截取到接近限制的长度,留3个字符给省略号
            $truncated = mb_substr($clean_title, 0, $max_length - 3, 'UTF-8');
            // 找到最后一个空格,确保截断在单词末尾
            $last_space = strrpos($truncated, ' ');
            if ($last_space !== false) {
                $truncated = substr($truncated, 0, $last_space);
            }
            return $truncated . '...';
        }
    }
    return $title;
}
add_filter('the_title', 'custom_truncate_title', 10, 2);

核心优势:

  • 后台编辑仍能看到完整标题,不影响后续内容维护
  • 避免中文/多字节字符截断乱码
  • 不会出现“WordPress 长标...”这种生硬的截断,用户体验更好

二、批量修改数据库标题(永久生效,彻底优化)

如果想直接把数据库里的长标题改成65字符以内(强烈建议先备份数据库!),可以用下面的批量处理代码:

function batch_truncate_post_titles() {
    global $wpdb;
    $max_length = 65;
    
    // 筛选出已发布、标题超标的文章
    $posts = $wpdb->get_results("
        SELECT ID, post_title 
        FROM {$wpdb->posts} 
        WHERE post_type = 'post' 
        AND post_status = 'publish' 
        AND CHAR_LENGTH(post_title) > {$max_length}
    ");
    
    if ($posts) {
        foreach ($posts as $post) {
            $clean_title = strip_tags($post->post_title);
            
            if (mb_strlen($clean_title) > $max_length) {
                $truncated = mb_substr($clean_title, 0, $max_length - 3, 'UTF-8');
                $last_space = strrpos($truncated, ' ');
                if ($last_space !== false) {
                    $truncated = substr($truncated, 0, $last_space);
                }
                $new_title = $truncated . '...';
                
                // 更新数据库中的标题
                $wpdb->update(
                    $wpdb->posts,
                    array('post_title' => $new_title),
                    array('ID' => $post->ID),
                    array('%s'),
                    array('%d')
                );
                
                // 可选:同步更新文章slug,保持和标题一致
                wp_update_post(array(
                    'ID' => $post->ID,
                    'post_name' => sanitize_title($new_title)
                ));
            }
        }
        echo '批量截断完成!共处理了' . count($posts) . '篇文章';
    } else {
        echo '没有需要截断的文章';
    }
}
// 执行函数:访问任意前端页面一次后,立刻删掉下面这行代码!
add_action('init', 'batch_truncate_post_titles');

必看提醒:

  1. 先备份数据库! 批量修改数据库有风险,出错了能及时恢复
  2. 执行完务必删除add_action('init', 'batch_truncate_post_titles');,不然每次访问网站都会重复执行
  3. 不需要同步slug的话,注释掉wp_update_post那部分即可

三、额外SEO优化:新文章标题防超标

为了避免以后再出现标题过长的问题,可以给后台加个发布验证,当标题超65字符时弹出提醒:

// 发布时验证标题长度
function validate_post_title_length($data, $postarr) {
    $max_length = 65;
    if ($data['post_type'] == 'post' && $data['post_status'] == 'publish') {
        $title_length = mb_strlen(strip_tags($data['post_title']));
        if ($title_length > $max_length) {
            add_filter('redirect_post_location', function($location) use ($title_length, $max_length) {
                return add_query_arg('title_too_long', "标题长度为{$title_length},超过限制的{$max_length}字符", $location);
            });
        }
    }
    return $data;
}
add_filter('wp_insert_post_data', 'validate_post_title_length', 10, 2);

// 后台显示提醒通知
function admin_title_length_notice() {
    if (isset($_GET['title_too_long'])) {
        ?>
        <div class="notice notice-warning is-dismissible">
            <p><?php echo esc_html($_GET['title_too_long']); ?>请修改后重新发布!</p>
        </div>
        <?php
    }
}
add_action('admin_notices', 'admin_title_length_notice');

内容的提问来源于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 09:05:59