【问题标题】:jQuery plugin not loading in WordPressjQuery插件未在WordPress中加载
【发布时间】:2013-08-21 13:08:58
【问题描述】:

我已经在functions.php 中成功地将 jQuery 链接到 WordPress。但是,center.js 似乎没有加载。我这么说是因为控制台给了我这个错误:Uncaught TypeError: Object [object Object] has no method 'center'

我在header.php 中调用所有带有<?php wp_head(); ?> 的脚本。这是我的functions.php:

<?php

function add_google_jquery() {
   if ( !is_admin() ) {
      wp_deregister_script('jquery');
      wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"), false);
      wp_enqueue_script('jquery');
   }
}
add_action('wp_print_scripts ', 'add_google_jquery');

// just added jQuery

function add_google_center() {
   if ( !is_admin() ) {
      wp_register_script('center', ("http://jquerydevbrazil.googlecode.com/svn/trunk/jquery.center.js"), false);
      wp_enqueue_script('center');
   }
}
add_action('wp_print_scripts_center ', 'add_google_center');


set_post_thumbnail_size( 800, 600, 1 );

function PA_stylesheet() {
    wp_enqueue_style( 'pa-style', get_stylesheet_uri() );

}

add_action( 'wp_enqueue_scripts', 'PA_stylesheet' );

function PA_javascript() {
    wp_enqueue_script( 'custom-script',
        get_stylesheet_directory_uri() . '/tran.js',
        array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'PA_javascript' );

?>

有人能帮忙解决这个问题吗?

【问题讨论】:

  • 可以粘贴一下trans.js的内容吗?这是你的“中心”方法被调用的地方吗?
  • Wordpress 在 wp-includes\js\jquery 中带有自己的 jQuery,为什么要针对旧版本进行更改?它可能会破坏其他插件或主题。

标签: php jquery wordpress plugins


【解决方案1】:

您不应该使用 wp_print_scripts。请改用 wp_enqueue_scripts。

http://codex.wordpress.org/Plugin_API/Action_Reference/wp_print_scripts

(这是为了绑定您的 add_google_jquery 和 add_google_center 函数)

所以,您需要做的就是将 'wp_print_scripts' 替换为 'wp_enqueue_scripts' 就可以了

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多