这个add_image_size( $name, $width, $height, $crop ) 函数足够优雅,可以使用相同的$name. 这很简单overwrites the existing value:
$_wp_additional_image_sizes[$name] = array( \'width\' => absint( $width ), \'height\' => absint( $height ), \'crop\' => (bool) $crop );
因此,这意味着覆盖父主题对自定义图像大小的定义所需要做的就是确保调用
add_image_size() 火灾
after 父主题的调用。
假设父主题确实如此:
function parent_theme_setup() {
add_image_size( \'name\', 500, 200, true );
add_image_size( \'new-name\', 400, 300, true );
}
add_action( \'after_setup_theme\', \'parent_theme_setup\' );
然后父主题的
add_image_size() 在
after_setup_theme 挂钩,默认优先级(即。
10).
(注意:如果调用没有包装在回调中,则会在plugins_loaded 挂钩,优先10.)
以下是关键部分:the child Theme\'s functions.php file is parsed before the parent Theme\'s, 所以如果你用同样的钩子add_image_size() 调用时,它们将被父主题的add_image_size() 呼叫。
解决方案是使用更晚的挂钩或更低的优先级,以确保子主题add_image_size() 调用在父主题之后激发。
无论父主题如何激发调用,这都应该起到作用:
function child_theme_setup() {
add_image_size( \'name\', 400, 300, true );
}
add_action( \'after_setup_theme\', \'child_theme_setup\', 11 );
请注意,我们使用相同的
after_setup_theme 挂钩,但使用较低优先级(即。
11 而不是
10. 回调按从较高优先级(较低编号)到较低优先级(较高编号)的顺序触发,从优先级开始
0. 所以回调连接到优先级
11 将在挂接到优先级的回调后激发
10.
还请注意after_setup_theme 钩子本身在plugins_loaded 钩子,因此此回调仍将覆盖父主题,即使父主题是_doing_it_wrong() 通过不将此类调用包装在适当的回调中,并连接到适当的挂钩中。