WordPress 你好世界的例子
示例
定制程序的基本概念是,管理员可以实时预览对其站点的更改,然后将其永久添加。
可以将以下内容复制并粘贴到主题functions.php文件中:
添加名为的定制程序部分MyFirstSection
添加称为HelloWorldColor允许管理员选择颜色的自定义程序设置
为此添加一个css规则,.hello-world以选择的颜色作为响应,#000000如果未选择任何内容,则默认为。设置将放在<style>末尾的标签中<head>。
function mytheme_customize_register( $wp_customize ) {
$wp_customize->add_section( 'my_first_section_id' , array(
'title' => __( 'My First Section', 'mytheme' ),
'priority' => 30,
) );
$wp_customize->add_setting( 'hello_world_color' , array(
'default' => '#000000',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link_color', array(
'label' => __( 'Hello World Color', 'mytheme' ),
'section' => 'my_first_section_id',
'settings' => 'hello_world_color',
) ) );
}
add_action( 'customize_register', 'mytheme_customize_register' );
function mytheme_customize_css()
{
?>
<style type="text/css">
.hello-world { color: #<?php echo get_theme_mod('hello_world_color', '000000'); ?>; }
</style>
<?php
}
add_action( 'wp_head', 'mytheme_customize_css');