oracle 子主题,WordPress子主题:覆盖函数

在我的父主题中, 我有一个没有初始语句的函数:

if (!function_exists(... etc...

如何在子主题中用同名的函数替换它?如果我将函数创建到我的functions.php中, 由于存在两个具有相同名称的函数, 这会给我一个错误。

预先感谢你的回答。

#1

这似乎为我工作:

function fileExistsInChildTheme($file_path){

$file_directory = get_template_directory();

if(file_exists($file_directory . "-child" . $file_path)){

return $file_directory .= "-child" . $file_path;

}

return $file_directory .= $file_path;

}

require ( fileExistsInChildTheme('/includes/functions.php') );

require ( fileExistsInChildTheme('/includes/theme-options.php') );

require ( fileExistsInChildTheme('/includes/hooks.php') );

require ( fileExistsInChildTheme('/includes/version.php') );

#2

子主题function.php文件在父主题函数文件之前加载, 因此在子主题中重新声明该函数时不会出现致命错误。这就是为什么你的父主题使用function_exists检查的原因。

也许你在钩子之后声明了子主题中的功能(例如init)?

这是有关此内容的法典文档:http://codex.wordpress.org/Child_Themes#Referencing_.2F_Inclusion_Files_in_Your_Child_Theme

#3

我认为如果你添加相同的功能名称, 则它取自子主题功能, 然后取自父。

对于前。

儿童主题

if ( ! function_exists( 'function_name' ) ) {

function function_name(){

echo 'This is child theme';

}

}

家长主题

if ( ! function_exists( 'function_name' ) ) {

function function_name(){

echo 'This is parent theme';

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值