我正在创建flowmaster主题的子主题。我有一个问题,以覆盖父函数。函数存在于父节点的主题中:
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');
得到这个错误:
致命错误:Cannot redeclare pt_loop_shop_columns(中声明C: ' xampp '根' futuratab ' wp-content ' ' flowmaster-child '显然也主题:44)在C: ' xampp '根' futuratab ' wp-content ' ' flowmaster ' woofunctions.php主题第9行
请帮助。由于
首先执行子主题的函数,然后执行父主题的函数。使用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;
}
你不能在PHP中重新定义一个函数,但是你可以解钩旧的函数,用不同的名字钩新函数。比如:
remove_filter('loop_shop_columns', 'pt_loop_shop_columns');
add_filter('loop_shop_columns', 'pt_loop_shop_columns_2');
你可以在你的子主题上尝试
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');
或
你可以在已有的函数
上使用钩子function pt_loop_shop_columns() {
//code goes here
}
$hook = 'get_options'; // the function name you're filtering
add_filter( $hook, 'pt_loop_shop_columns' );
或
最后一个方法是
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);