【问题标题】:Why is jquery not working with wordpress enqueue?为什么 jquery 不能与 wordpress enqueue 一起使用?
【发布时间】:2012-07-05 10:21:03
【问题描述】:

我只是从这个方法切换了我在主题中排队 jquery 的方式:

function my_init() {
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');
    wp_enqueue_script('jquery');
}
}
add_action('wp_enqueue_scripts', 'my_init');

到这里:

function my_scripts_method() {
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');

function my_admin_scripts_method() {
wp_enqueue_script('jquery');
}
add_action('admin_enqueue_scripts', 'my_admin_scripts_method');

旧版本使用1.7.2,wordpress'内置版本是1.7.1。我尝试将 1.7.1 的地址复制并粘贴到第一个版本中,但它还是一样的。该链接在 1.7.1 版本的源代码中,但 jquery 不起作用。有什么想法可能导致这种情况吗?

网站网址:www.brainbuzzmedia.com/themes/vertex/

【问题讨论】:

    标签: jquery wordpress


    【解决方案1】:

    我不知道这里到底有什么问题,但试试这个。

    function wptuts_scripts_with_jquery()
    {
        // Register the script like this for a plugin:
        wp_register_script( 'custom-script', plugins_url( '/js/custom-script.js', __FILE__ ), array( 'jquery' ) );
        // or
        // Register the script like this for a theme:
        wp_register_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.js', array( 'jquery' ) );
    
        // For either a plugin or a theme, you can then enqueue the script:
        wp_enqueue_script( 'custom-script' );
    }
    add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );
    

    更多info...

    【讨论】:

      【解决方案2】:
      function my_scripts_method() {
      if (!is_admin()) {
          wp_enqueue_script('jquery');
       }
      }
      add_action('wp_enqueue_scripts', 'my_scripts_method');
      
      function my_admin_scripts_method() {
      // you don't need to load jquery here, because its automatically loaded in the adminby default 
      }
      add_action('admin_enqueue_scripts', 'my_admin_scripts_method');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-29
        • 1970-01-01
        • 2018-03-09
        • 2021-06-14
        • 2012-10-09
        • 2020-03-18
        • 2017-11-21
        • 2019-04-11
        相关资源
        最近更新 更多