删除所有主题CSS&;来自wp_head Wordpress的JS,但不删除wp admin toollbar
在模板中,wp_head()函数添加了一系列样式和脚本。 这会覆盖主题的其他CSS,从而产生问题 我尝试使用一个函数来删除CSS和js。它工作了,但也从WP管理工具栏中删除了CSS删除所有主题CSS&;来自wp_head Wordpress的JS,但不删除wp admin toollbar,css,wordpress,head,Css,Wordpress,Head,在模板中,wp_head()函数添加了一系列样式和脚本。 这会覆盖主题的其他CSS,从而产生问题 我尝试使用一个函数来删除CSS和js。它工作了,但也从WP管理工具栏中删除了CSS function clear_styles_and_scripts() { global $wp_scripts; global $wp_styles; foreach( $wp_scripts->queue as $handle ) : wp_dequeue_script(
function clear_styles_and_scripts() {
global $wp_scripts;
global $wp_styles;
foreach( $wp_scripts->queue as $handle ) :
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
endforeach;
foreach( $wp_styles ->queue as $handle ) :
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
}
add_action( 'wp_enqueue_scripts', 'clear_styles_and_scripts', 100 );
虽然我不建议将所有脚本和样式出列,但您可以检查文件句柄以忽略该特定文件的出列:
foreach( $wp_styles ->queue as $handle ) :
// allows admin bar to be added
if( 'admin-bar' === $handle ) continue;
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
这起作用了
function clear_styles_and_scripts() {
global $wp_scripts;
global $wp_styles;
$styles_to_keep = array("wp-admin", "admin-bar", "dashicons", "open-sans");
foreach( $wp_styles ->queue as $handle ) :
if ( in_array($handle, $styles_to_keep) ) continue;
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
}
add_action( 'wp_enqueue_scripts', 'clear_styles_and_scripts', 100 );
把所有的脚本和样式都排出来是个坏主意。为什么不把不需要的文件排出来?