我是WordPress编码新手,从stackexchange学习到这一点,我正在尝试从头创建一个WordPress网站,在标题中我想显示联系信息,例如电话号码,我想在自定义中创建一个字段,并想在标题中显示值。php。有没有关于这方面的指南或教程?任何帮助都将不胜感激。非常感谢*不想使用插件
如何在Customize中创建一个字段并在header.php中显示?
1 个回复
最合适的回答,由SO网友:Hendrik Vlaanderen 整理而成
add_action( \'customize_register\', \'theme_customize_register\' );
/**
* Register individual settings through customizer\'s API.
*
* @param WP_Customize_Manager $wp_customize Customizer reference.
*/
function theme_customize_register( $wp_customize ) {
// You can first start by adding a section
$wp_customize->add_section(
\'theme_options\',
array(
\'title\' => __( \'Title of your section\', \'domain\' ),
\'capability\' => \'edit_theme_options\',
\'description\' => __( \'Some description\', \'domain\' ),
\'priority\' => 160,
)
);
$wp_customize->add_setting(
\'field_name\',
array(
\'type\' => \'textarea\',
\'sanitize_callback\' => \'theme_sanitize_text\', // You can sanitize the text afterwards through this function
\'capability\' => \'edit_theme_options\',
)
);
}
在API docs 您还可以找到可以使用的其他字段以及如何修改这些字段。要访问您的设置,您可以使用
get_theme_mod(\'field_name\');
确保将其添加到插件或子主题。