在模板中,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( $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 );
最佳答案
这有效。
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 );
关于css - 从 wp_head Wordpress 中删除所有主题 CSS 和 JS,但不删除 wp 管理工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57765325/