[英]Wordpress child theme: override functions
在我的父主題中,我有一個沒有初始語句的函數:
if (!function_exists(... etc...
如何在子主題中用同名的函數替換它? 如果我將函數創建到我的functions.php中,由於存在兩個具有相同名稱的函數,這會給我一個錯誤。
預先感謝您的回答。
這似乎為我工作:
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') );
子主題function.php文件在父主題函數文件之前加載,因此在子主題中重新聲明該功能時不會出現致命錯誤。 這就是為什么您的父主題使用function_exists
檢查的原因。
也許您在掛鈎(例如init)之后聲明了子主題中的功能?
這是與此有關的法典文檔: http : //codex.wordpress.org/Child_Themes#Referencing_.2F_Inclusion_Files_in_Your_Child_Theme
我認為,如果您添加相同的功能名稱,則它取自子主題功能,然后取自父。
對於前。
兒童主題
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';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.