php - 覆盖子主题中的模块
问题描述
我正在修改主题中的模块并将其添加到子主题中。该模块不是页面模板,而是一个 PHP 文件。我将文件放在子主题中与父主题相同的层次结构中,但 WordPress 没有选择子主题文件。如何让它发挥作用?
解决方案
子主题旨在覆盖模板。通常包含模板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,而是扩展它。
你可以做些什么来解决你的问题:
- 在您的子主题 functions.php 中
require()
包含新功能 PHP 文件include()
add_filter
使用或连接到您的父主题功能add_action
- 如果所有这些都没有帮助,请打扰父主题开发人员以增加其可扩展性
推荐阅读
- abp - ABP/Swashbuckle - 使用 Swashbuckle CLI 生成 swagger 文档
- flutter - 如何实际检查 null 以符合 Dart 中的 Sound null-safety
- python - 我们可以将消息从 Pub/sub 主题拉到 Angular 吗?
- kubernetes - GCP - 我可以将 Compute Engine 用于生产 MySQL 数据库吗
- javascript - 是否可以使用 React '获取'元素样式并将其渲染到 DOM 上?
- javascript - reactjs中使用firebase身份验证的登录功能
- php - 我需要帮助使用 PHP/jQuery/AJAX 获取动态行数的 MySQL 数据
- javascript - 如何仅为 Javascript onclick 回调选择元素的背景空白?
- java - 在Java中为Oracle SQL IN子句将列表转换为字符串
- python - 要以交替方式将列表中的项目添加到两个新列表中,为什么这段代码不起作用?(它说 player_1 没有定义)