【问题标题】:Conditionally load js file based on Wordpress theme file基于Wordpress主题文件有条件加载js文件
【发布时间】:2012-05-15 13:43:33
【问题描述】:

如何确定是否调用了 Wordpress 主题文件?比如每次加载archive.php,我想有条件的在页脚添加一个js文件。

我目前在functions.php中使用类似的东西来加载全局js。如何调整以便根据调用的 Wordpress 文件加载额外的 js?

//Use Google CDN to load jQuery
function use_google_jquery() {
    if (!is_admin()) {
        // comment out the next two lines to load the local copy of jQuery
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', false, '1.7.2', true);
        wp_enqueue_script('jquery');

        // load a JS file from my theme: js/default.js
        wp_enqueue_script('my_script', get_bloginfo('template_url') . '/js/default.js', array('jquery'), '1.0', true);      
    }   }   add_action('init', 'use_google_jquery');

编辑和注意:JS on Specific Wordpress Page 适用于给定页面,但我正在寻找给定模板文件,例如 archive.php 并加载到页脚中。

【问题讨论】:

    标签: jquery wordpress wordpress-theming


    【解决方案1】:

    使用以下函数is_single()、is_archive()、is_page()、is_home()

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 2017-11-18
      • 2018-02-24
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 2020-09-21
      • 2018-07-21
      • 2017-06-24
      相关资源
      最近更新 更多