简体   繁体   中英

Using Jquery with wordpress theme?

I am currently using wordpress 3.4.1 I would like to embedd jquery in my own wordpress theme but i am unable to do it. Tried max possible solutions available on net but still not convincingly able to implement it. Please help with the simple example!!

try Below code.

below code added your function.php in your theme

add_action('wp_head', 'add_myJquery');

function add_myJquery()
{
?>
    <script src="<?php bloginfo( 'template_url' ); ?>/js/jquery-latest.js" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(document).ready(function() {

            //write your jquery code Here.
        });

    </script>
<?    
}

Or

below code added in between html head tag in your theme header.php file

<script src="<?php bloginfo( 'template_url' ); ?>/js/jquery-latest.js" type="text/javascript"></script>

For best compatibility, use the version of jQuery that WordPress comes with (loading a specific version of jQuery may break your plugins in the future):

Add to function.php:

function insert_jquery(){
   wp_enqueue_script('jquery');
}
add_filter('wp_head','insert_jquery');

Or if you want to load it from Google CDN:

function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');

Read more here: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

The following links can also be used:

http://code.jquery.com/jquery-latest.min.js - Most recent version

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js - latest within ver 1.x family

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js - latest within ver 1.7.x family

http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js - latest within ver 1.8.x family

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM