我第一次使用主题定制API时遇到了一些问题。我在跟踪a tutorial, 但我似乎做错了什么。我试图允许用户通过WordPress主题自定义选项更改页面上的文本。
我已将所有挂钩添加到functions.php
文件和单独的。调用了js文件theme-customizer.js
.
我当前的设置如下所示:
functions.php
<?php
function tcx_register_theme_customizer( $wp_customize ) {
$wp_customize->add_section(
\'tcx_display_options\',
array(
\'title\' => \'Display Options\',
\'priority\' => 200
)
);
$wp_customize->add_setting(
\'tcx_footer_copyright_text\',
array(
\'default\' => \'All Rights Reserved\',
\'sanitize_callback\' => \'tcx_sanitize_copyright\',
\'transport\' => \'postMessage\'
)
);
$wp_customize->add_control(
\'tcx_footer_copyright_text\',
array(
\'section\' => \'tcx_display_options\',
\'label\' => \'Copyright Message\',
\'type\' => \'text\'
)
);
}
add_action( \'customize_register\', \'tcx_register_theme_customizer\' );
function tcx_sanitize_copyright( $input ) {
return strip_tags( stripslashes( $input ) );
}
function tcx_customizer_live_preview() {
wp_enqueue_script(
\'tcx-theme-customizer\',
get_template_directory_uri() . \'/library/js/theme-customizer.js\',
array( \'jquery\', \'customize-preview\' ),
\'1.0.0\',
true
);
}
add_action( \'customize_preview_init\', \'tcx_customizer_live_preview\' );
theme-customizer.js
(function( $ ) {
"use strict";
wp.customize( \'tcx_footer_copyright_text\', function( value ) {
value.bind( function( to ) {
$( \'.hero-content header h2\' ).text( to );
});
});
})( jQuery );
page-landing.php
<div class="hero-content">
<header>
<h2><?php echo get_theme_mod( \'tcx_footer_copyright_message\' ); ?></h2>
</header>
</div>
当我去定制主题时,首先我看不到
\'default\'
文本当我在输入字段中键入时,键入的文本将显示在页面上。但是,当我单击“保存”并刷新页面时,更改似乎没有保存。文本区域为空白。
知道我做错了什么吗。非常感谢您的帮助。提前感谢!