【问题标题】:Include js for conditional statements in Wordpress plugin在 Wordpress 插件中包含用于条件语句的 js
【发布时间】:2014-05-07 08:07:20
【问题描述】:

我必须在我正在开发的 wordpress 插件中包含一个样式表和 js,同时牢记 Wordpress 插件标准。 如何将这行代码添加到取决于浏览器类型的前端。 对于我包含的其他样式,我使用了 wp_register_style 和 wp_enqueue_style 函数。 但在这种特殊情况下,我有疑问。

 <!--[if lt IE 9]>   
  <link href="<?php echo plugins_url('/plugin_name/js/htmlxx.css'); ?>" rel="stylesheet" type="text/css" />

 <![endif]--> 

谁能给我一些建议。


更新--

我想知道如何在wordpress插件中为javascript添加条件语句

【问题讨论】:

    标签: javascript css wordpress


    【解决方案1】:

    试试这个。

    function your_plugin_register_scripts() {
      global $wp_styles; // use global $wp_styles to add conditional wrapper.
      if (!is_admin()) {
        wp_register_style( 'main-style', '/path/to/your/css/style.css', array(), '', 'all' );
        wp_enqueue_style( 'main-style' );
        $wp_styles->add_data( 'main-style', 'conditional', 'lt IE 9' );
    
      }
    }
    
    add_action( 'wp_enqueue_scripts', 'your_plugin_register_scripts' ); 
    

    【讨论】:

    • 脚本的情况下如何使用?
    • @user3230561 哪个脚本?
    • 我的意思是,如果我必须在 lt IE 9 条件下加载 javascript 文件,那我该怎么办?
    • @user3230561 相同。只需将 wp_register_style() 替换为 wp_register_script()。 $wp_styles->add_data() 方法添加了条件包装器,因此无论您使用脚本还是样式都无关紧要
    • 我按照步骤进行了,但是.. 我可以看到脚本也在 Firefox 浏览器中被添加,这不应该发生对吗?
    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2012-07-16
    • 2016-02-29
    • 1970-01-01
    相关资源
    最近更新 更多