gpt4 book ai didi

php - 在 WordPress 中 Hook jQuery 脚本的正确方法

转载 作者:行者123 更新时间:2023-12-01 04:07:05 25 4
gpt4 key购买 nike

我有一个 Wordpress 站点,并且有一个 .js 文件,我希望将其链接起来,以便我可以调用脚本。经过大量研究,我发现这必须通过functions.php 中的钩子(Hook)来完成。我尝试了各种变体,但无法使官方方法发挥作用,但我找到了一种可行的方法,但只是在此过程中破坏了网站的其余部分。

我做错了什么?我知道 get_stylesheet_directory_uri()get_template_directory_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
);
}

“完全不推荐”的方式有效,但会破坏网站的其余部分:

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');

一如既往,非常感谢任何帮助。谢谢大家

更新

回显 get_stylesheet_directory_uri() 后,我可以看到我的 URL 需要是相对的,现在应该如下所示,但它仍然不起作用。

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
);
}

最佳答案

PHP

您需要调用函数add_jquery_script();,然后才能期望输出任何内容 - 非常简单,很容易被忘记。

JQuery

由于引用错误,脚本破坏了您的代码,var Price 定义应移至函数内部,以便它位于函数的作用域 内。 http://learn.jquery.com/javascript-101/scope/

$(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);
}

关于php - 在 WordPress 中 Hook jQuery 脚本的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25977447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com