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

如何修改WooCommerce评分输出,无需CSS仅保留星级移除附带文字

WooCommerce修改星级评分输出方案

你可以直接使用函数自带的官方过滤器实现需求,不需要修改核心文件、不需要额外CSS控制,操作步骤如下:

  • 进入WordPress后台,依次打开「外观」-「主题文件编辑器」,选择你当前使用的主题(推荐使用子主题避免后续主题更新覆盖修改),找到主题目录下的functions.php文件
  • 在functions.php文件末尾添加以下代码:
add_filter( 'woocommerce_product_get_rating_html', 'customize_rating_html_output', 10, 2 );
function customize_rating_html_output( $rating_html, $rating ) {
    if ( $rating > 0 ) {
        // 仅保留星级渲染必须的结构,移除所有文字说明
        $custom_html = '<div class="star-rating">';
        $custom_html .= '<span style="width:' . ( ( $rating / 5 ) * 100 ) . '%"></span>';
        $custom_html .= '</div>';
        return $custom_html;
    }
    return '';
}
  • 保存文件后刷新前台页面即可生效。

代码逻辑说明:直接拦截wc_get_rating_html返回的输出内容,完全重构HTML结构,只保留控制星级显示的宽度属性和必要CSS类,彻底移除了原代码中的title提示文字、评分数值、「out of 5」等所有文字说明,完全匹配需求。

内容的提问来源于stack exchange,提问作者Chris Wiseman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 07:45:04