因此,我尝试了几种不同的方法来覆盖父主题内的方法,但我一点运气都没有。
结构如下:
主题/
-wp starter
--自定义\\u标题。php
-wp starter子级--custom\\u头。php
我在父custom\\u头中有一个方法。php如下所示:
function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( \'custom-header\', apply_filters( \'wp_bootstrap_starter_custom_header_args\', array(
        \'default-image\'          => \'\',
        \'default-text-color\'     => \'fff\',
        \'width\'                  => 1000,
        \'height\'                 => 250,
        \'flex-height\'            => true,
        \'wp-head-callback\'       => \'wp_bootstrap_starter_header_style\',
    ) ) );
}
add_action( \'after_setup_theme\', \'wp_bootstrap_starter_custom_header_setup\' );
 现在。。我希望能够在我的孩子体内调用该方法
custom_header.php 并替代宽度和高度。
以下是一些尝试:
增加了行动优先级(不起作用):
function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( \'custom-header\', apply_filters( \'wp_bootstrap_starter_custom_header_args\', array(
        \'default-image\'          => \'\',
        \'default-text-color\'     => \'fff\',
        \'width\'                  => 1000,
        \'height\'                 => 500,
        \'flex-height\'            => true,
        \'wp-head-callback\'       => \'wp_bootstrap_starter_header_style\',
    ) ) );
}
add_action(\'after_setup_theme\', \'wp_bootstrap_starter_custom_header_setup\', 20);
 重命名了方法并添加了优先级(无效):
function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( \'custom-header\', apply_filters( \'wp_bootstrap_starter_custom_header_args\', array(
        \'default-image\'          => \'\',
        \'default-text-color\'     => \'fff\',
        \'width\'                  => 1000,
        \'height\'                 => 500,
        \'flex-height\'            => true,
        \'wp-head-callback\'       => \'wp_bootstrap_starter_header_style\',
    ) ) );
}
add_action(\'after_setup_theme\', \'wp_bootstrap_starter_custom_header_setup\', 20);
 添加了具有优先级的初始化操作调用(无效):
function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( \'custom-header\', apply_filters( \'wp_bootstrap_starter_custom_header_args\', array(
        \'default-image\'          => \'\',
        \'default-text-color\'     => \'fff\',
        \'width\'                  => 1000,
        \'height\'                 => 500,
        \'flex-height\'            => true,
        \'wp-head-callback\'       => \'wp_bootstrap_starter_header_style\',
    ) ) );
}
add_action(\'after_setup_theme\', \'wp_bootstrap_starter_custom_header_setup\' );
add_action(\'init\', \'wp_bootstrap_starter_custom_header_setup\', 15);
 所以我试着
remove_action(\'after_setup_theme\', \'wp_bootstrap_starter_custom_header_setup\'); 没有结果。
 
                    最合适的回答,由SO网友:Tom J Nowell 整理而成
                    不能只是在子主题中重新声明函数或调用add_action 第二次。它并没有取代它,而是增加了第二个挂钩。因此,您没有覆盖它,而是复制了原始文件。子主题替代仅适用于模板。
此外,通过添加第二个定义wp_bootstrap_starter_custom_header_setup 您已经声明了两次该函数,这将生成一个PHP致命错误。不能有两个同名函数。
因此,首先,我们需要重命名您的函数,以便有有效的PHP:
function sams_custom_header_setup() {
下一个
add_action 添加操作。它没有替换动作的概念。如果你打电话
add_action 5次,该功能将运行5次。
因此,让我们为新设置添加操作:
add_action(\'after_setup_theme\', \'sams_custom_header_setup\' );
但是请记住,原来的函数也添加了,所以现在两者都将运行!因此,请删除原始文件:
remove_action( \'after_setup_theme\', \'wp_bootstrap_starter_custom_header_setup\' );
TLDR:
您不能“覆盖”操作但您可以删除它们并添加新操作来替换它们停止使用相同名称创建多个函数!这是无效的PHP,它会破坏一切。子主题允许您覆盖通过WP加载的模板,而不是任意的PHP文件、函数、挂钩等。编辑:
似乎父主题中使用的函数apply_filters, 您可以在wp_bootstrap_starter_custom_header_args 过滤并修改阵列本身。您不需要在替换函数或调用add_theme_support