我试图存储模板url(bloginfo(template_url);
) 转换成一个变量,这样我就可以在JavaScript中使用它了。然而,我意识到这是不可能的,有人提醒我wp_localize_script
说它可以做我想做的事。我查阅了法典,但我仍然不确定如何实现这一点。
以下是我目前掌握的情况:
作用
function starter_scripts() {
wp_enqueue_script( \'jquery\' );
wp_localize_script( \'my_script\', \'templateUrl\', array(
\'templateUrl\' => template_url()
) );
}
add_action( \'wp_enqueue_scripts\', \'starter_scripts\' );
JS公司jQuery(document).ready(function($){
// Fade in Contact background
$(\'body.page-template-page-contact #content\').css(\'background\', \'url(templateUrl + bg-contact.jpg) 50% 0% no-repeat fixed\').fadeIn(2000);
});
我哪里出错了?更新时间:
function starter_scripts() {
wp_enqueue_style( \'starter-style\', get_stylesheet_uri() );
wp_enqueue_script( \'jquery\' );
wp_enqueue_script( \'includes\', get_template_directory_uri() . \'/js/min/includes.min.js\', \'\', \'\', true );
wp_localize_script( \'includes\', \'site\', array(
\'url\' => site_url(\'/\'),
\'theme_path\' => get_template_directory_uri(),
\'templateUrl\' => template_url()
) );
}
add_action( \'wp_enqueue_scripts\', \'starter_scripts\' );
JS公司jQuery(document).ready(function($){
// Fade in Contact background
$(\'body.page-template-page-contact #content\').css(\'background\', \'url(site.templateUrl/img/bg-contact.jpg) 50% 0% no-repeat fixed\').fadeIn(2000);
});