Php 在Wordpress中钩住jQuery脚本的正确方法

Php 在Wordpress中钩住jQuery脚本的正确方法,php,jquery,wordpress,Php,Jquery,Wordpress,我有一个Wordpress网站,有一个.js文件,我想链接起来,这样我就可以调用脚本了。经过大量研究,我发现这必须通过functions.php中的挂钩来完成。我尝试过各种各样的变化,但无法让官方方法发挥作用,但我找到了一种方法,它确实有效,但在这个过程中会破坏网站的其余部分 我做错了什么?我知道get\u stylesheet\u directory\u uri() 以下是不起作用的“官方”方式: function add_jquery_script() { wp_enqueue_scri

我有一个Wordpress网站,有一个.js文件,我想链接起来,这样我就可以调用脚本了。经过大量研究,我发现这必须通过functions.php中的挂钩来完成。我尝试过各种各样的变化,但无法让官方方法发挥作用,但我找到了一种方法,它确实有效,但在这个过程中会破坏网站的其余部分

我做错了什么?我知道
get\u stylesheet\u directory\u uri()

以下是不起作用的“官方”方式:

function add_jquery_script() {
  wp_enqueue_script(
    'my-script', // name your script so that you can attach other scripts and de-register, etc.
     get_stylesheet_directory_uri() . 'http://<site>.com/wp-content/themes/dt-the7/custom-scripts.js', // this is the location of your script file
     array('jquery') // this array lists the scripts upon which your script depends
  );
}
PHP 您需要调用函数
add_jquery_script()在它可以输出任何东西之前-非常简单,很容易忘记

JQuery 由于引用错误,脚本正在破坏您的代码,
var price
定义应在函数内部移动,因此它位于函数的
范围内。


当你说“不工作”是什么意思,它是否标记了一个php/javascript/头错误,脚本是否链接输出但不调用,输出是否错误,位置是否错误,什么?@Edward it没有做任何事情。就好像脚本没有被调用一样。我刚刚意识到,在
get\u stylesheet\u directory\u uri()
上出现回音后,我的URL应该是相对的,但即使现在它仍然不起作用。ThanksOK-所以默默地失败了,你有错误报告吗<代码>错误报告(E_全部)@Edward我刚刚在函数上方添加了那一行,它没有报告任何错误。我刚想到。。。创建函数后,我使用的脚本实际上并没有调用函数
add\u jquery\u script()
——请不要告诉我我有那么笨!?嗯,我要说的是,哈哈,呃,需要调用函数才能执行,是的。这有时很难找到,特别是在其他人的框架中,所以不,你不是哑巴。
function add_jquery_script() {
    echo '<script type="text/javascript" src="http://<site>/wp-content/themes/dt-the7/custom-scripts.js"></script>' . "\n";
}
add_action('wp_head', 'add_jquery_script');
function add_jquery_script() {
  wp_enqueue_script(
    'my-script', // name your script so that you can attach other scripts and de-register, etc.
     get_stylesheet_directory_uri() . '/custom-scripts.js', // this is the location of your script file
     array('jquery') // this array lists the scripts upon which your script depends
  );
}
$(document).ready(function () {
    refresh();
    $('#bittrex-price').load(bittrexPrice);
});

function refresh() {
    var price = "[domain hidden]/realtime/bittrex-realtime.php";
    setTimeout(function (price) {
        $('#bittrex-price').fadeOut('slow').load(price, params).fadeIn('slow');
        $('#bittrex-vol').fadeOut('slow').load(price, params2).fadeIn('fast');
        refresh();
    }, 1000);
}