使用WordPress自定义程序。要添加WordPress颜色选择器,只需使用Customizer Manager和每个Customizer对象的add\\uMethods。代码可用于函数中。php文件或单独的自定义程序特定文件中。
function diwp_customizer_add_colorPicker( $wp_customize){
// Add New Section: Background Colors
$wp_customize->add_section( \'diwp_color_section\', array(
\'title\' => \'Background Color\',
\'description\' => \'Set Color For Background\',
\'priority\' => \'40\'
));
// Add Settings
$wp_customize->add_setting( \'diwp_background_color\', array(
\'default\' => \'#370d07\',
));
// Add Controls
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, \'diwp_background_color\', array(
\'label\' => \'Choose Color\',
\'section\' => \'diwp_color_section\',
\'settings\' => \'diwp_background_color\'
)));
}
add_action( \'customize_register\', \'diwp_customizer_add_colorPicker\' );
要显示更改的颜色,请在函数中添加此代码。php:
// displays custom background color
function diwp_generate_theme_option_css() {
$backColor = get_theme_mod( \'diwp_background_color\' );
if ( ! empty( $backColor ) ):
?>
<style type="text/css" id="diwp-theme-option-css">
.container {
background: <?php echo esc_html($backColor); ?>
}
</style>
<?php
endif;
}
add_action( \'wp_head\', \'diwp_generate_theme_option_css\' );
Original code here
或者您可以在这个答案中看到如何使用jQuery更改颜色
https://wordpress.stackexchange.com/a/360001/151351