所以我猜您已经在子主题中注册了自定义标题functions.php
文件:
add_theme_support( \'custom-header\' );
下一个
locate the customizer section 并为主题定制器附加您自己的设置/控件。假设我们要注册
background_position
和
background_size
并将其存储在阵列中
custom_header_meta
:
add_action( \'customize_register\', \'wpse_customize_custom_header_meta\' );
function wpse_customize_custom_header_meta( \\WP_Customize_Manager $wp_customize ) {
$wp_customize->add_setting(
\'custom_header_meta[background_position]\',
array(
\'default\' => \'left\',
\'capability\' => \'edit_theme_options\',
\'type\' => \'option\'
)
);
$wp_customize->add_control(
\'custom_header_background_position\',
array(
\'settings\' => \'custom_header_meta[background_position]\',
\'label\' => __( \'Background position:\', \'textdomain\' ),
\'section\' => \'header_image\',
\'type\' => \'select\',
\'choices\' => array(
\'right\' => \'right\',
\'left\' => \'left\'
)
)
);
$wp_customize->add_setting(
\'custom_header_meta[background_size]\',
array(
\'default\' => \'auto\',
\'capability\' => \'edit_theme_options\',
\'type\' => \'option\'
)
);
$wp_customize->add_control(
\'custom_header_background_size\',
array(
\'settings\' => \'custom_header_meta[background_size]\',
\'label\' => __( \'Background size:\', \'textdomain\' ),
\'section\' => \'header_image\',
\'type\' => \'select\',
\'choices\' => array(
\'auto\' => \'auto\',
\'cover\' => \'cover\',
\'contain\' => \'contain\'
)
)
);
}
我想你会明白的;您可以随意使用此字段或添加更多字段。检查
examples provided in the Codex 供参考。
之后,我们可以使用
wp_add_inline_style
功能:
add_action( \'wp_enqueue_scripts\', \'wpse_rowling_inline_styles\' );
function wpse_rowling_inline_styles() {
// Get custom header meta from customizer with defaults.
$default_header_meta = array(
\'background_position\' => \'left\',
\'background_size\' => \'auto\'
);
$header_meta = get_option( \'custom_header_meta\', $default_header_meta );
// Render header meta as CSS parameters.
$header_styles = \'\';
foreach ( $header_meta as $key => $val ) {
$header_styles .= str_replace( \'_\', \'-\', $key ) . \':\' . $val . \';\';
}
// Render header image as CSS parameters.
if ( get_header_image() ) {
$header_image = get_theme_mod( \'header_image_data\' );
$header_styles .= \'background-image:url(\' . $header_image->url . \');\';
$header_styles .= \'width:\' . (string) $header_image->width . \'px;\';
$header_styles .= \'height:\' . (string) $header_image->height . \'px;\';
}
// Finally render CSS selector with parameters.
$custom_css = ".header_image { $header_styles }";
wp_add_inline_style( \'rowling_style\', $custom_css );
}
现在,您应该可以在类中看到元素的样式
header_image
在源代码中以及实时预览中的渲染版本中,除了rowling自定义项之外。
Further reading: 同时检查主题支持
custom-background
因为这为图像样式提供了开箱即用的基本支持(默认情况下,在自定义程序中不可用)。