2012-05-26 140 views
1

在我父主題我有一個功能,而不初始聲明:WordPress的兒童主題:覆蓋功能

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

我如何用我的子主題同名的函數代替它呢? 如果我將函數創建到我的functions.php中,它會給我一個錯誤,因爲有兩個函數具有相同的名稱。

非常感謝您的回答。

+0

http://stackoverflow.com/questions/4725194/php-override-existing-function – Robus

回答

0

子主題function.php文件被加載之前父主題功能文件,因此你不應該得到重新聲明子主題中的函數的致命錯誤。這就是爲什麼你的父母主題使用function_exists檢查。

也許你是在鉤子(例如init)之後在子主題中聲明函數?

下面是關於這個食品文檔:http://codex.wordpress.org/Child_Themes#Referencing_.2F_Including_Files_in_Your_Child_Theme

+0

的問題是,這個函數,父主題是不是使用function_exists檢查 – Avionicom

+0

哦,我誤讀了「沒有」的詞 - 對不起評論抱歉。我唯一的想法是爲該函數執行'remove_action'或'remove_filter',然後鉤住另一個函數。不知道這是否適用於你的情況。 –

+0

我知道,謝謝你一樣。我想我必須通過創建一個新的功能來換一種方式。 – Avionicom

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')); 
0

我認爲,如果你添加的功能相同的名字,那麼它從兒童主題功能拍攝則從父母那裏拿走。

例如,

兒童主題

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

@Avionicom你可以請檢查我的答案 –