2015-05-15 90 views
1

我正在創建flowmaster theme的子主題。我有一個問題來覆蓋父函數。 功能在父母的主題存在:覆蓋Wordpress的父主題功能

add_filter('loop_shop_columns', 'pt_loop_shop_columns'); 
function pt_loop_shop_columns(){ 
    if ('layout-one-col' == pt_show_layout()) return 4; 
    else return 3; 
} 

我在兒童主題

if (! function_exists('pt_loop_shop_columns')) : 
function pt_loop_shop_columns(){ 
    global $wp_query; 
    if ('layout-one-col' == pt_show_layout()) return 4; 
    else return 4; 
} 
endif; 
add_filter('loop_shop_columns', 'pt_loop_shop_columns'); 

得到這個錯誤添加函數:

Fatal error: Cannot redeclare pt_loop_shop_columns() (previously declared in C:\xampp\htdocs\futuratab\wp-content\themes\flowmaster-child\functions.php:44) in C:\xampp\htdocs\futuratab\wp-content\themes\flowmaster\woofunctions.php on line 9

請help.Thanks

回答

1

功能是先執行父主題的。在父主題中應該使用function_exists進行檢查。

爲了克服這個問題,你可以刪除父主題的鉤子並將你的自定義函數掛接到同一個篩選器。

remove_filter('loop_shop_columns', 'pt_loop_shop_columns'); 

add_filter('loop_shop_columns', 'custom_pt_loop_shop_columns'); 

function custom_pt_loop_shop_columns(){ 
    global $wp_query; 
    if ('layout-one-col' == pt_show_layout()) return 4; 
    else return 4; 
} 
+0

謝謝,爲您的reply.function_exists未添加到父主題中。將此代碼添加到子主題函數中,錯誤已消失。但其未在行中返回4個項目,其中仍有3個項目在行中。 –

+0

然後嘗試降低優先級,'add_filter('loop_shop_columns','custom_pt_loop_shop_columns',20);' – Nilambar

+0

偉大的,它的工作,謝謝。 –

0

你可以不重新定義PHP中的函數,但可以解除舊函數的掛鉤並掛鉤n用一個不同的名字命名。喜歡的東西:

remove_filter('loop_shop_columns', 'pt_loop_shop_columns'); 
add_filter('loop_shop_columns', 'pt_loop_shop_columns_2'); 
0

你可以試試這個在您的子主題

function pt_loop_shop_columns() { 

//NEW CODE IN HERE/////////////////////////////// 

return apply_filters('pt_loop_shop_columns', $link, $id); 
} 

add_filter('attachment_link', 'pt_loop_shop_columns'); 

OR

你可以在現有的功能使用鉤

function pt_loop_shop_columns() { 
//code goes here 
} 

$hook = 'get_options'; // the function name you're filtering 
add_filter($hook, 'pt_loop_shop_columns'); 

OR

而最後一種方法是子主題的

function remove_thematic_actions() { 
remove_action('thematic_header','thematic_blogtitle',3); 
} 
// Call 'remove_thematic_actions' during WP initialization 
add_action('init','remove_thematic_actions'); 

// Add our custom function to the 'thematic_header' phase 
add_action('thematic_header','fancy_theme_blogtitle', 3);