我在键入(实时预览)时,在输入字段中键入的文本无法显示在页面上。
当我点击save(保存)和refresh(刷新)时,更改被保存,只是出于某种原因,它们不会在我键入时出现。你知道为什么现场预览不起作用吗?我相信我做的一切都是正确的。
functions.php
<?php
function tcx_register_theme_customizer( $wp_customize ) {
$wp_customize->add_section(
\'tcx_notification_options\',
array(
\'title\' => \'Notification Bar\',
\'priority\' => 201
)
);
$wp_customize->add_setting(
\'tcx_notification_text\',
array(
\'default\' => \'Free International Shipping on Orders Over £100\',
\'sanitize_callback\' => \'tcx_sanitize_notification_text\',
\'transport\' => \'postMessage\'
)
);
$wp_customize->add_control(
\'tcx_notification_text\',
array(
\'section\' => \'tcx_notification_options\',
\'label\' => \'Notification Text\',
\'type\' => \'text\'
)
);
add_action( \'customize_register\', \'tcx_register_theme_customizer\' );
function tcx_sanitize_notification_text( $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_notification_text\', function(value) {
value.bind(function(to) {
$(\'.notification-title\').text(to);
});
});
})( jQuery );
header.php
<strong class="notification-title"><?php echo get_theme_mod( \'tcx_notification_text\' ); ?></strong>