简体   繁体   English

wordpress-注册,排队和添加javascript functions.php

[英]wordpress - registering, enqueuing, and adding javascript functions.php

I know that there are several posts on this here, but having gone through a ton of them, I am still having problems. 我知道这里有几篇文章,但是经过大量讨论之后,我仍然遇到问题。 So - up front apologies on adding another question on something that seems so straightforward but I'm new to this and stuck! 所以-对于在看起来如此简单但我不熟悉的新问题上添加一个问题表示歉意!

That being said - I have soundclick.js. 话虽这么说-我有soundclick.js。 It lives in my theme's js folder. 它位于我主题的js文件夹中。 It works pasted straight into the header - but i need it in my functions.php file like all the other good little javascripts. 它可以直接粘贴到标题中-但是我需要在我的functions.php文件中像其他所有出色的小javascripts一样使用它。

function wpana_js() {

  wp_register_script('soundclick', get_template_directory_uri() . '/js/soundclick.js', array(), '0', false);
  wp_enqueue_script('soundclick');
} 

add_action('wp_enqueue_scripts', 'wpana_js');

Code goes in - javascript shows up in the page source, but then my calls to the javascript to get these sounds to play on a user click don't work anymore. 代码进入了-javascript出现在页面源中,但是随后我对javascript的调用使这些声音在用户单击上播放不再起作用。 (the calls worked just dandy when the javascript was pasted directly into the header) (当将javascript直接粘贴到标头中时,调用仅在丹迪上起作用)

Am i missing something obvious on getting the javascript back into working status? 我是否缺少使javascript恢复工作状态的明显方法? Thanks! 谢谢!

update i fixed the problem - apparently i was queuing correctly, but my problem lay in the js file itself. 更新我解决了这个问题-显然我在正确排队,但是我的问题出在js文件本身。 there was a basic problem with how i made the file. 我如何制作文件存在一个基本问题。 so it was showing up but not working. 所以它出现了,但是没有用。 thanks all for taking a look :) 谢谢大家看看:)

I would inspect the order in which your scripts are being loaded when 我会检查您的脚本在加载时的顺序

  • it works by pasting it directly into the header, versus 通过将其直接粘贴到标题中来工作,
  • it's loaded via wp_enqueue_script() as you've done in functions.php 就像在functions.php一样,它是通过wp_enqueue_script()加载的

Is this library getting loaded after your JavaScript code that attempts to use it? 尝试使用该库的JavaScript代码之后会加载该库吗? Is soundclick.js a jQuery plugin? soundclick.js是jQuery插件吗?

If you need to load some scripts after others, ensure you define the dependent scripts in the array parameter like so (for example, if this script requires jQuery) 如果您需要先加载其他脚本,请确保在array参数中定义依赖脚本,例如(例如,如果此脚本需要jQuery)

wp_register_script('soundclick', get_template_directory_uri() . '/js/soundclick.js', array('jquery'), '0', false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM