覆盖子主题中的模块

时间:2018-05-04 11:43:17

标签: php wordpress wordpress-theming extend child-theming

我正在修改主题中的模块并将其添加到子主题中。此模块不是页面模板,而是PHP文件。我将文件放在子主题中与父主题中相同的层次结构中,但WordPress没有选择子主题文件。如何使它工作?

1 个答案:

答案 0 :(得分:0)

子主题旨在覆盖 模板 。模板通常包含在get_template_part()中,基本上使用以下功能:

/**
 * Retrieve the name of the highest priority template file that exists.
 *
 * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
 * so that themes which inherit from a parent theme can just overload one file.
 *
 * @since 2.7.0
 *
 * @param string|array $template_names Template file(s) to search for, in order.
 * @param bool         $load           If true the template file will be loaded if it is found.
 * @param bool         $require_once   Whether to require_once or require. Default true. Has no effect if $load is false.
 * @return string The template filename if one is located.
 */
function locate_template($template_names, $load = false, $require_once = true ) {
    $located = '';
    foreach ( (array) $template_names as $template_name ) {
        if ( !$template_name )
            continue;
        if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
            $located = STYLESHEETPATH . '/' . $template_name;
            break;
        } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
            $located = TEMPLATEPATH . '/' . $template_name;
            break;
        } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
            $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
            break;
        }
    }

    if ( $load && '' != $located )
        load_template( $located, $require_once );

    return $located;
}

如您所见,在模板路径之前检查STYLESHEETPATH(子主题路径)。但是你必须像文件一样包含文件。

无法使用子主题覆盖任意PHP文件。你也不要覆盖父母' functions.php,而不是你扩展它。

您可以采取哪些措施来解决问题:

  1. 在您的子主题function.php中包含require()include()的新功能PHP文件
  2. 使用add_filteradd_action
  3. 挂钩您的父主题功能
  4. 如果所有这些都无济于事,那就麻烦父主题开发人员增加其可扩展性