覆盖 Wordpress 父主题功能 [英] Override a Wordpress parent theme function
问题描述
我正在创建 flowmaster 主题的子主题.我在覆盖父函数时遇到问题.该函数存在于父主题中:
I am creating a child theme of flowmaster theme.I have a problem to override the parent function. The function exists in parent's theme:
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;
}
我在子主题中添加了一个功能
I add a function in child theme
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');
遇到这个错误:
致命错误:无法重新声明 pt_loop_shop_columns()(以前是声明于C:\xampp\htdocs\futuratab\wp-content\themes\flowmaster-child\functions.php:44)在C:\xampp\htdocs\futuratab\wp-content\themes\flowmaster\woofunctions.php第 9 行
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
请帮忙.谢谢
推荐答案
先执行子主题的功能,然后执行父主题的功能.应该在父主题中使用 function_exists
进行检查.
Function of child theme is executed first and then parent theme's. Checking using function_exists
should have been done in parent theme.
为了克服这个问题,您可以移除父主题的挂钩并将自定义函数挂钩到相同的过滤器.
To overcome this you can remove parent theme's hook and hook your custom function to same filter.
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;
}
这篇关于覆盖 Wordpress 父主题功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!