importing jquery into wordpress

2019-09-16 11:42发布

问题:

Can you import jquery into wordpress when you're not using <?php wp_head(); ?>

i'm just beginning with wordpress and didnt include that in my theme, when i add it now it meses some things up and since i only have to add 1 more thing with jquery i dont want to do the hassle of fixing those things, so i just need to add jquery without the the tag. Any ideas?

回答1:

You can manually add jQuery from the Google CDN like this in header.php, above an other calls to jQuery libraries:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

But realize that not using wp_head in your theme means you will need to manually add links to JS and CSS files for most plugins, as they hook into WP via wp_head and sometimes get_footer, too.

If things don't work right, use Firebug with Firefox to determine what JS is loading and if there are conflicts and errors.



回答2:

Use the wp_enqueue_script() function. It's best to use this and let wordpress worry about including the jquery scripts so that you don't end up having the themes/plugins/whatever each including their own versions.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script



回答3:

i'm just beginning with wordpress and didnt include that in my theme, when i add it now it meses some things up and since i only have to add 1 more thing with jquery i dont want to do the hassle of fixing those things, so i just need to add jquery without the the tag. Any ideas?

I would highly recommend that you keep <?php wp_head(); ?> not having wp_head will cause more problems down the road. For example, your friend will not be able to use most plugins.

If you want to use Googles jQuery you just need to de register WordPress's jQuery:

if( !is_admin()){
   wp_deregister_script('jquery'); 
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false, '1.4.2'); 
   wp_enqueue_script('jquery');
}


标签: wordpress