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

如何避免WooCommerce后台产品标题受自定义代码影响?

解决WooCommerce后台产品列表标题截断问题

针对你遇到的后台产品标题被截断、现有代码仅生效于前端的问题,可通过以下两种方式结合解决:

1. 修改标题过滤逻辑,确保后台显示完整标题

修改你现有的自定义代码,加入后台环境判断,让后台产品列表直接返回完整标题,同时保留前端的截断逻辑:

add_filter( 'the_title', 'custom_woo_product_title_display', 10, 2 );
function custom_woo_product_title_display( $title, $id ) {
    // 后台产品列表页面直接返回完整标题
    if ( is_admin() && get_post_type( $id ) === 'product' ) {
        return $title;
    }
    // 前端非产品单页,保留原截断逻辑
    if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
        return wp_trim_words( $title, 4, '...' );
    }
    // 其他场景返回原标题
    return $title;
}

2. 调整后台列表列宽,避免因空间不足截断

有时候标题本身完整,但列表列宽过窄会强制截断,可添加CSS调整标题列的宽度与换行规则:

add_action( 'admin_head', 'custom_woo_product_list_css' );
function custom_woo_product_list_css() {
    // 仅在产品列表页面生效
    $screen = get_current_screen();
    if ( $screen->id !== 'edit-product' ) return;
    ?>
    <style>
        .wp-list-table .column-title {
            min-width: 350px; /* 可根据实际需求调整宽度 */
            white-space: normal; /* 允许标题换行,避免强制省略 */
        }
    </style>
    <?php
}

使用方法

将上述两段代码添加到当前主题的functions.php文件中,或通过代码片段插件(如Code Snippets)导入执行。

内容的提问来源于stack exchange,提问作者Gamo SA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 17:17:12