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

无需修改插件文件,重写WordPress插件工时日期翻译函数

解决方案

1. 核心问题分析

原插件通过if (!function_exists())包裹函数定义,插件加载优先级高于主题,导致你在functions.php中的同名函数无法执行。同时原函数使用英文日期标识(如monday)作为数组键,模板直接输出该键,因此显示英文日期。

2. 替换工时参数过滤器

在主题functions.php中添加以下代码,移除原插件的过滤器并替换为自定义逻辑,将日期标识映射为中文名称:

// 移除原插件的工时参数过滤器
remove_filter( 'montagne_core_filter_working_hours_template_params', 'montagne_core_set_working_hours_template_params' );
// 添加自定义过滤器函数
add_filter( 'montagne_core_filter_working_hours_template_params', 'custom_montagne_core_set_working_hours_template_params' );

/**
 * 自定义工时参数设置函数,替换日期为中文
 *
 * @param array $params
 * @return array
 */
function custom_montagne_core_set_working_hours_template_params( $params ) {
    // 定义日期标识与中文名称的映射
    $days = array(
        'monday'    => esc_html__( '星期一', 'your-theme-domain' ), // 替换为你的主题文本域
        'tuesday'   => esc_html__( '星期二', 'your-theme-domain' ),
        'wednesday' => esc_html__( '星期三', 'your-theme-domain' ),
        'thursday'  => esc_html__( '星期四', 'your-theme-domain' ),
        'friday'    => esc_html__( '星期五', 'your-theme-domain' ),
        'saturday'  => esc_html__( '星期六', 'your-theme-domain' ),
        'sunday'    => esc_html__( '星期日', 'your-theme-domain' ),
    );

    $new_params = array();
    foreach ( $days as $day_slug => $day_label ) {
        // 获取后台设置的对应日期工时
        $option = montagne_core_get_post_value_through_levels( 'qodef_working_hours_' . $day_slug );
        // 以中文名称为键,工时为值存入新参数
        $new_params[ $day_label ] = ! empty( $option ) ? esc_attr( $option ) : '';
    }

    return $new_params;
}

3. 修复特殊日期匹配问题

后台存储的特殊日期是英文标识,需转换为中文名称才能与模板中的日期匹配,继续添加以下代码:

// 移除原插件的特殊工时参数过滤器
remove_filter( 'montagne_core_filter_working_hours_special_template_params', 'montagne_core_set_working_hours_special_template_params' );
// 添加自定义特殊工时参数过滤器
add_filter( 'montagne_core_filter_working_hours_special_template_params', 'custom_montagne_core_set_working_hours_special_template_params' );

/**
 * 自定义特殊工时参数设置函数,转换特殊日期标识为中文
 *
 * @param array $params
 * @return array
 */
function custom_montagne_core_set_working_hours_special_template_params( $params ) {
    $special_days = montagne_core_get_post_value_through_levels( 'qodef_working_hours_special_days' );
    $special_text = montagne_core_get_post_value_through_levels( 'qodef_working_hours_special_text' );

    if ( ! empty( $special_days ) ) {
        $special_days = array_filter( (array) $special_days, 'strlen' );
        // 保持与上面一致的日期映射
        $day_map = array(
            'monday'    => esc_html__( '星期一', 'your-theme-domain' ),
            'tuesday'   => esc_html__( '星期二', 'your-theme-domain' ),
            'wednesday' => esc_html__( '星期三', 'your-theme-domain' ),
            'thursday'  => esc_html__( '星期四', 'your-theme-domain' ),
            'friday'    => esc_html__( '星期五', 'your-theme-domain' ),
            'saturday'  => esc_html__( '星期六', 'your-theme-domain' ),
            'sunday'    => esc_html__( '星期日', 'your-theme-domain' ),
        );
        // 将存储的英文标识转换为中文名称
        $special_days = array_map( function( $slug ) use ( $day_map ) {
            return isset( $day_map[ $slug ] ) ? $day_map[ $slug ] : $slug;
        }, $special_days );
    }

    $params['special_days'] = $special_days;
    $params['special_text'] = esc_attr( $special_text );

    return $params;
}

注意事项

  • 将代码中的your-theme-domain替换为你的主题实际文本域(可在主题style.css中找到,格式为Text Domain: your-theme)。
  • 若需翻译为其他语言,直接替换中文日期名称为对应语言即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 00:09:51