个性化阅读
专注于IT技术分析

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)
未经允许不得转载:srcmini » WordPress子主题:覆盖函数

评论 抢沙发

评论前必须登录!