2015-05-15 92 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);