我有一个Wordpress网站,并有一个.js文件,我希望将其链接起来,以便可以调用该脚本。经过大量研究,我发现这必须通过functions.php中的钩子来完成。我尝试了各种变体,但是无法使用正式的方法,但是我发现了一种有效的方法,但是在此过程中破坏了网站的其余部分。
我究竟做错了什么?我知道有关父主题和子主题的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
定义移入函数内部,使其位于函数的scope
中。
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);
}
jQuery Ajax加载仅适用于单个单词变量 - php在我的PHP文件中,我将一些变量从输入框传递到链接中,该链接通过jQuery的ajax load函数在其URL中使用该变量加载页面。整个系统运行良好,但仅适用于单字变量。每当涉及到空格时,我的Ajax通话都会中断。我假设这是一个编码问题,但是我也有一些疑问。基本上,它归结为我的PHP文件中的这一行代码,这导致了混乱。是否有可能找出格式是否正确,或者为什么在多…
PHP strtotime困境 - php有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00…
PHP-全局变量的性能和内存问题 - php假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…
jQuery PHP在函数中传递值与实时单击? - php我正在尝试通过onclick传递一些值,这对我来说要快得多。但是我需要使用某种“实时”单击,并且正在查看.on()或.delegate()。但是,如果执行这些操作中的任何一个,则在followme()中传递这些值似乎很难。有没有我看不到的某种方法? <div class='btn btn-mini follow-btn' data-…