Php 重写Wordpress父主题函数

Php 重写Wordpress父主题函数,php,wordpress,Php,Wordpress,我正在创建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_e

我正在创建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');
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');
出现以下错误:

致命错误:无法重新声明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行


请提供帮助。谢谢你不能在PHP中重新定义函数,但是你可以解开旧函数的钩子,用不同的名字钩住新函数。比如:

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

你可以在你的孩子主题上试试这个

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

先执行子主题的函数,然后执行父主题的函数。应该在父主题中使用
函数\u 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;
}

谢谢,谢谢你的回复。没有在父主题中添加函数“存在”。在子主题函数中添加此代码错误消失了。但它没有在行中返回4项。在行中仍然返回3项。然后尝试降低优先级,
Add\u filter('loop\u shop\u columns','custom\u pt\u loop\u shop\u columns',20)为什么是20?它与20一起工作。我可以添加任何其他数字吗?下面的11尝试过,但上面的11不工作。默认优先级为
10
。您可以分配大于10的号码。谢谢您的帮助。