我正在尝试设计自己的 wordpress
主题,但在让我的 JavaScript
工作时遇到问题,而且我研究过的任何内容似乎都没有答案。如果有人能帮助我,我将不胜感激!
我做过的事情:
在functions.php中将我的javascript文件排入队列:
function sorenTheme_scripts() {
wp_enqueue_style('style', get_stylesheet_uri());
wp_register_script( 'soren-script', get_template_directory_uri() . '/js/soren-script.js', array( 'jquery', 'jquery-ui-core' ), '1', true );
wp_enqueue_script('soren-script');
}
add_action('wp_enqueue_scripts', 'sorenTheme_scripts');
在目录{template文件夹}/js中创建一个简单的javaScript文件,名为soren-script.js,目前只包含一个非常简单的警报测试
alert('Hello World');
就是这样。当我尝试直接使用 script 标签
将警报放入 index.php 文件
中时,警报按预期出现,但是当我将其移动到 js 文件时,我什么也得不到。我需要在 .php
或 .js
文件中添加一些内容吗?
编辑:当我查看开发人员控制台时,源文件包括 jquery、我的 css 文件等,但不包括 soren-script.js
所以我想让 soren-script.js 显示可以解决问题,但我不知道该怎么做,我认为排队会使其自动显示为源,就像我的风格一样片材
最佳答案
您的模板中可能没有 wp_footer() 函数。由于在此 Hook 中代码将被加载,因此如果您尚未定义 Hook ,则代码不会出现。
$in_footer (boolean) (optional) Normally, scripts are placed in of the HTML document. If this parameter is true, the script is placed before the end tag. This requires the theme to have the wp_footer() template tag in the appropriate place. Default: false
引用:http://codex.wordpress.org/Function_Reference/wp_enqueue_script
请注意,get_footer()
和 wp_footer()
是 2 different things :
The get_footer() template tag is a custom wrapper for the locate_template() function, used to include a template-part file within a template file. The get_footer() template tag is part of the WordPress template system, and is used primarily by the Theme itself, to specify the footer.php or footer-{slug}.php file to include in the current template.
The wp_footer() template tag is a custom wrapper for the wp_footer action hook, which is invoked via do_action( 'wp_footer' ). The wp_footer() template tag is part of the WordPress Hooks API, and is used primarily by Plugins, to inject scripts in the site HTML footer.
wp_footer 钩子(Hook)通常放置在结束正文标记之前:
<?php
wp_footer();
?>
</body>
关于javascript - WordPress 主题 : JavaScript not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760473/