开发者

what is the best way to include more than one js file into wordpress theme ?

I am trying to add many js files to my newly created theme (I 开发者_如何学Goam new to wordpress theming ) I was trying to do it in this way :

function includejQuery() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        $jqueryPath = get_bloginfo('template_directory') . '/js/jquery-1.6.2.min.js';
        wp_register_script('jquery', $jqueryPath);
        wp_enqueue_script('jquery');
   }
}
function includeddAccordionUI() {
    if (!is_admin()) {
        wp_deregister_script('ddaccordion');
         $ddaccordionPath = get_bloginfo('template_directory') . '/js/ddaccordionUI.js';
          wp_register_script('ddaccordion', $ddaccordionPath , array("jquery"));
          wp_enqueue_script('ddaccordion'); 
    }
}

add_action('init', 'includejQuery');
add_action('init', 'includeddAccordionUI');

the above mentioned idea work just fine to include one file but if you include 2 file php will go into infinite loop (or the page won't stop loading )

is there any better way to include many js files ?


Use the action wp_enqueue_scriptswhen enqueuing scripts front end. This way you don't have to check if you're in admin. Use admin_enqueue_scripts when you actually want to load it in admin. This is not only to get rid of your bug, but also to get rid of the conditional. Also use only one function and load all js from that function, to prevent possible duplication of code (such as your current conditional). Now if this does not work, it has nothing to do with Wordpress and probably is due to the js. A last recommendation is to use the included js files, the usually suffice!

Codex page for wp_enqueue_script: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜