我有一个脚本,在声明了一些变量之后,它必须在我的页脚中运行。如果我只是将代码直接放在页脚文件中,它就会起作用,但我认为最佳实践要求我应该通过函数来实现这一点。php和wp\\u本地化脚本。
不幸的是,这不起作用;脚本始终在标题中输出。你知道我错在哪里吗?提前感谢您的帮助!
add_action( \'wp_enqueue_scripts\', \'mytheme_enqueue_front_page_scripts\' );
function mytheme_enqueue_front_page_scripts() {
wp_register_script(\'flowplayer_object\',
get_bloginfo(\'stylesheet_directory\') . \'/_/js/flowplayer/flowplayer-object-creator.js\' );
// last \'true\' in wp_enqueue_script should force this into footer, right?
wp_enqueue_script( \'flowplayer_object\',\'\',\'\',true, true );
$data = array( \'my_stylesheet_path\' => __( get_bloginfo(\'stylesheet_directory\') ) );
wp_localize_script( \'flowplayer_object\', \'my_data\', $data );
}
最合适的回答,由SO网友:mor7ifer 整理而成
您应该将其设置为与寄存器一起显示在页脚中,因此您的代码应该如下所示:
wp_register_script(
\'flowplayer_object\',
get_bloginfo( \'stylesheet_directory\' ) . \'/_/js/flowplayer/flowplayer-object-creator.js\',
array(), // these are your dependencies, if you need jQuery or something, it needs to go in that array
false, // set a version if you want
true // this will put it in the footer
);
wp_enqueue_script( \'flowplayer_object\' );