没有这样的过滤器,所以你所做的就是尽你所能做到最好。但是,您可以通过在应用过滤器的同时创建包装模板函数的函数来进行一些优化:
add_filter(
\'filter_template_part_args\',
function( $args, $slug, $name = null ) {
$args = wp_parse_args(
$args,
[
\'id\' => esc_attr( basename( $slug ) ),
\'tag\' => \'div\',
\'class\' => \'\',
\'container\' => \'container\',
\'row\' => \'row\',
]
);
return $args;
}
)
function mytheme_get_template_part( $slug, $name = null, $args = [] ) {
$args = apply_filters( \'filter_template_part_args\', $args, $slug, $name );
get_template_part( $slug, $name, $args );
}
function mytheme_get_header( $name = null, $args = [] ) {
$args = apply_filters( \'filter_template_part_args\', $args, \'header\', $name );
get_header( $name, $args );
}
那么你也可以这样做
get_sidebar()
和
get_footer()
, 您应该了解所有主要模板功能:
function mytheme_get_footer( $name = null, $args = [] ) {
$args = apply_filters( \'filter_template_part_args\', $args, \'footer\', $name );
get_footer( $name, $args );
}
function mytheme_get_sidebar( $name = null, $args = [] ) {
$args = apply_filters( \'filter_template_part_args\', $args, \'sidebar\', $name );
get_sidebar( $name, $args );
}