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';
  }
}

来源:

https://www.srcmini02.com/67615.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_36860.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?