css - 从 wp_head Wordpress 中删除所有主题 CSS 和 JS,但不删除 wp 管理工具栏

标签 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( $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/

相关文章:

adobe - 如何通过 Adob​​e CQ 更改视口(viewport)设置?

html - 如何修复 HTML 中带有 Logo 的文本对齐方式

javascript - 区分纯触摸事件和纯手势事件?

mysql - 使用 mysqli_connect() 将 Wordpress 连接到 Google App Engine 上的 Cloud SQL

mysql - 寻求帮助将数据库复制到另一个域

php - 在自定义帖子类型 WordPress 中插入来自自定义帖子类型的数据

jquery - 使用 jquery 在响应式布局中显示当前行 div 下方隐藏的 div

html - 透明粘性导航栏不起作用

php - 在 <head> 中编写 PHP

javascript - html页面中的head标签在页面加载后以编程方式关闭