【问题标题】:Remove all theme CSS & JS from wp_head Wordpress but not remove the wp admin toollbar从 wp_head Wordpress 中删除所有主题 CSS 和 JS,但不删除 wp 管理工具栏
【发布时间】:2020-01-05 23:39:25
【问题描述】:

在模板中,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 );

【问题讨论】:

  • 像这样将所有脚本和样式出队是个坏主意。为什么不直接将不需要的文件出队?

标签: css wordpress head


【解决方案1】:

这行得通。

  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 );

【讨论】:

    【解决方案2】:

    虽然我不建议将所有脚本和样式出队,但您可以检查文件句柄以忽略该特定文件的出队:

    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;
    

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多