I'm trying to overwrite following code in the child theme. I want to load file /inc/soundtheme-admin.php from child folder. I copied same code snippet in child-theme/functions.php and also copied the inc/soundtheme-admin.php. I also replaced the get_template_directory() to get_stylesheet_directory() but its still loading parent file.

Please correct me where I'm making a mistake. Thanks in advance.

// Code snippet from parent-theme/functions.php

if( function_exists('acf_add_options_page') ) {

    require get_template_directory() . '/inc/soundtheme-admin.php';

    $paper_themes = 
    acf_add_options_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Theme',
        'menu_slug'     => 'theme-general-settings',
        'capability'    => 'edit_posts',
        'redirect'  => false,
        'autoload' => false,
        'icon_url' => 'dashicons-carrot'
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Options',
        'parent_slug'   => 'theme-general-settings',
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Layouts',
        'parent_slug'   => 'theme-general-settings',
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Code',
        'parent_slug'   => 'theme-general-settings',
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Supports',
        'parent_slug'   => 'theme-general-settings',
    ));
}
有帮助吗?

解决方案

The functions in your child theme will be loaded before the functions in the parent theme. This means that if your parent and child themes both have functions called my_function() which do a similar job, the one in the parent theme will load last, meaning it will override the one in the child theme.

~Guide to Functions and Child themes

Further:

Function Priority

If you're not using your own parent theme, or you're using a third party one without pluggable functions, you'll need another method.

When you write functions you can assign them a priority, which tells WordPress when to run them. You do this when adding your function to an action or filter hook. WordPress will then run the functions attached to a given hook in ascending order of priority, so those with higher numbers will run last.

Let's imagine the function in the parent theme isn't pluggable, and looks like this:

<?php
    function parent_function() {
        // Contents for your function here.
    }
    add_action( 'init', 'parent_function' );
?>

This means the function in your child theme would look like this:

    <?php
    function child_function() {
        // Contents for your function here.
    }
    add_action( 'init', 'child_function', 15 );
    ?>

That should get you started...

其他提示

Above comment definitely helped. The declaration was in the different file for acf_add_options_page function and you need to include that file child-functions.php file too.

I just copied the code inside if block from parent-functions.php file and paste it in child-functions.php file including required files. That worked for me.

Hope it will help you too.

Thanks.

许可以下: CC-BY-SA归因
scroll top