WordPress child theme override a parent theme incl

2019-03-03 07:50发布

问题:

I'm creating a WordPress child theme and need to overwrite certain widget templates. I'm trying to use this method to override the parent theme call: The Right Way to Override Theme Functions.

However, I currently get this error:

Fatal error: Cannot redeclare hickory_homepage_load_widget() (previously declared in C:\wamp\www\greenpeaceNewBlog\wp-content\themes\gp-blog\inc\widgets\homepage_widget.php:8) in C:\wamp\www\greenpeaceNewBlog\wp-content\themes\hickory\inc\widgets\homepage_widget.php on line 10

The parent theme functions.php calls the templates like this:

include("inc/widgets/homepage_widget.php");

The homepage_widget.php file contains this:

add_action( 'widgets_init', 'hickory_homepage_load_widget' );

function hickory_homepage_load_widget() {
    register_widget( 'hickory_homepage_widget' );
}

I have a child theme duplicate widget directory (stylesheet_directory/inc/widgets/..) and a child theme functions.php. In my child functions.php, I have written this:

// Remove the default Thematic blogtitle function
function remove_hickory_widgets() {
    remove_action('widgets_init', 'hickory_homepage_load_widget');
}

// Call 'remove_thematic_actions' (above) during WP initialization
add_action('init','remove_hickory_widgets');

include("inc/widgets/homepage_widget.php");

Please help :)

Cheers

回答1:

Try this

add_action( 'init', 'remove_hickory_widgets' );

function remove_hickory_widgets() {
    remove_action('widgets_init', 'hickory_homepage_load_widget' );
    add_action( 'init', 'custom_widgets' );
}

function custom_widgets(){
    // your widget code here
}