Wordpress:如何从特定模板中注销脚本
Wordpress: How to deregister script from specific templates
我想知道如何排除以下模板的 google 地图 api 脚本,但在网站的其余部分加载:
function my_register_javascript() {
if ( !is_post_type_archive( 'impact' ) || !is_post_type_archive( 'deal' ) || !is_post_type_archive( 'local' ) || !is_tax( 'directory' )) {
wp_register_script( 'googlemapsapi', '//maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyD-DQkeStHf3xm9e0ibSPcVGDLlaSTwWEA®ion=US&language=EN');
wp_enqueue_script('googlemapsapi');
}
}
add_action( 'wp_enqueue_scripts', 'my_register_javascript', 100 );
最简单的方法是在模板文件代码中根据需要使用 https://codex.wordpress.org/Function_Reference/wp_deregister_script and or https://codex.wordpress.org/Function_Reference/wp_dequeue_script,然后再调用 get_header();
您可以在入队脚本周围添加一个条件,但如果您有一个实际的模板页面则没有必要 - 它会在每个其他页面上保存 运行 查询。
我想知道如何排除以下模板的 google 地图 api 脚本,但在网站的其余部分加载:
function my_register_javascript() {
if ( !is_post_type_archive( 'impact' ) || !is_post_type_archive( 'deal' ) || !is_post_type_archive( 'local' ) || !is_tax( 'directory' )) {
wp_register_script( 'googlemapsapi', '//maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyD-DQkeStHf3xm9e0ibSPcVGDLlaSTwWEA®ion=US&language=EN');
wp_enqueue_script('googlemapsapi');
}
}
add_action( 'wp_enqueue_scripts', 'my_register_javascript', 100 );
最简单的方法是在模板文件代码中根据需要使用 https://codex.wordpress.org/Function_Reference/wp_deregister_script and or https://codex.wordpress.org/Function_Reference/wp_dequeue_script,然后再调用 get_header();
您可以在入队脚本周围添加一个条件,但如果您有一个实际的模板页面则没有必要 - 它会在每个其他页面上保存 运行 查询。