我有一个自定义幻灯片,我使用jQuery开发。它在我的本地机器上工作得很完美,但是当我尝试将其转换到wordpress服务器时,它将无法工作......
这是我链接我的javascript文件的方式:
<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
<script type="text/javascript" src="<?php bloginfo('template_url') ?>/js/jQuery.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url') ?>/js/JQueryUI.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url') ?>/js/slider.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url') ?>/js/gallery.js"></script>
我还检查过javascript是否有效(像警报一样)。但是与jQuery相关的一切都没有。
迫切需要帮助。任何相关教程的提示或链接都可以。谢谢你提前!
答案 0 :(得分:6)
您应该在functions.php
文件中使用wp_enqueue_script()
,而不是header.php
。 (你要两次添加jQuery)
的functions.php:
function my_scripts_method() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui', get_template_directory_uri() . '/js/JQueryUI.js', );
wp_enqueue_script( 'slider', get_template_directory_uri() . '/js/slider.js' );
wp_enqueue_script( 'gallery', get_template_directory_uri() . '/js/gallery.js' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
您还应该注意到WordPress在noConflict mode中将jQuery排入队列,因此您需要noConflict wrappers才能使用$
:
jQuery(document).ready(function($) {
// your code here
});
然后你只需拨打wp_head()
,WordPress会自动将这些javascripts添加到你的页面。
答案 1 :(得分:1)
正如您在此处所见:Function Reference/wp head,在 In twentyten theme 示例中,他们添加了一条注释:
/* Always have wp_head() just before the closing </head> * tag of your theme, or you will break many plugins, which * generally use this hook to add elements to <head> such * as styles, scripts, and meta tags. */
这只是说你需要放wp_head();
函数才能关闭<head></head>
。
所以试着把这一行:
<?php wp_head(); ?>
作为关闭你网站中<head>
之前的最后一行。
我看到的另一个问题是你忘了用;
非常关键!
使用您在此处提供的代码,请使用以下代码进行更改:
<?php wp_enqueue_script("jquery"); ?>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jQuery.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/JQueryUI.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/slider.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/gallery.js"></script>
<?php wp_head(); ?>