2011-11-17 93 views
2

我一直在爲我的自定義(而不是一個插件)的WordPress的jQuery幻燈片。我一整天都在搜索如何在Wordpress中集成JavaScript,但似乎遇到了很多不同的方法。所以我的主要問題是哪種方式是編寫腳本的正確,最快的方式。使用JQuery和Wordpress的正確方法?

從我看到它看起來像我應該使用Wordpress入隊,以便該腳本只在需要的頁面上調用,在我的情況下是index.php。但我不確定正確的做法。另外,我應該添加一些東西到我的functions.php?

下面是腳本:

<script src="Js/js/jquery-1.6.1.min.js" type="text/javascript"></script> 
<script src="Js/js/jquery.zaccordion.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
    $("#example4 ul").zAccordion({ 
     slideWidth: 600, 
     width: 900, 
     height: 250, 
     timeout: 5000, 
     slideClass: "frame" 
    }); 
    }); 
</script> 

而這正是我現在在我的header.php:

<?php wp_enqueue_script('jquery'); ?> 
<?php wp_head();?> 
<script type="text/javascript" 
     src="<?php bloginfo("template_url"); ?>/js/jquery-1.6.1.min.js"></script> 
<script type="text/javascript" 
     src="<?php bloginfo("template_url"); ?>/js/jquery.zaccordion.js"></script> 

<script type="text/javascript"> 
    jQuery.noConflict(); 
    jQuery(document).ready(function() { 
    jQuery("#slider ul").zAccordion({ 
     slideWidth: 600, 
     width: 800, 
     height: 250, 
     timeout: 5000, 
     slideClass: "frame" 
    }); 
    }); 
</script> 

回答

0

我的建議(和解決方案):

WordPress的插件開發 - 初學者指南 Packtlib press

這是ar最好的書,非常清晰,簡單,並且有大量可用於jQuery的良好示例。

而且你有真正的東西寫WordPress插件,當大家應該做的;)

0

如果你不想創建一個插件,然後第二個最好的解決辦法是安裝Headspace2插件,它允許你添加任何JS或CSS文件到Wordpress Admin Edit頁面/郵件屏幕上的任何頁面。

另外我會把你的內聯JS變成一個外部JS文件(mycustom.js),然後像你做其他JS文件一樣包含它,但這是我個人的偏好。

0

這裏有一對夫婦的事情,我可以建議:

  1. 不包括你的.js,因爲你已經入列其中
  2. 使用jquery.zaccordion.js wp_register_script然後排隊像你註冊與jquery
  3. 你不需要包括jQuery.noConflict();因爲WordPress'jQuery已經聲明。只有當你包含你自己的jQuery時,你需要
0

你可以在你的主題文件中包含jQuery並將其稱爲functions.php文件。在我的情況下,腳本在js文件夾(your_theme/js /)中。

在WordPress中加載腳本的正確方法是使用函數wp_enqueue_script

在functions.php的在你的主題類型:

/** 
* Load jQuery script 
*/ 
if (!is_admin()) { 
    function register_my_jquery() { 
     wp_deregister_script('jquery'); 
     wp_register_script('jquery', get_bloginfo('template_directory') . "/js/jquery.min.js"); 
     wp_enqueue_script('jquery'); 
    } 
    add_action('init', 'register_my_jquery'); 
}