Enque script not working for jQuery in wordpress

2019-05-24 07:46发布

I always, always have trouble getting jQuery to work in wordpress.

I really need some clarification and explination on just how this thing works.

Here is my code and I can't seem to see what is wrong with it.

In the functions.php file:

function my_init() {
if (!is_admin()) {
    // comment out the next two lines to load the local copy of jQuery
    // wp_deregister_script('jquery'); 
    // wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2'); 
    wp_enqueue_script('jquery');
} } add_action('init', 'my_init');

And this is in my footer before I call my other jQuery scripts:

<?php wp_enqueue_script("jquery"); ?>

1条回答
够拽才男人
2楼-- · 2019-05-24 07:56

There're some answers in this post also.

I've encountered this also in the past, and I usually don't even use the wordpress own jquery if needed. Because the serverside google method is faster. This code below has to work.

functions.php

<?php

function google_jquery() {
    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', 'google_jquery');

?>

Make sure that wp_head(); is in your header.php file.

header.php

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title(); ?> <?php bloginfo( 'name' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen" />
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
<?php if ( is_singular() && get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply' ); ?>
<?php wp_head(); ?>
</head>

You don't need to call the other jquery scripts in the footer that way. If you're enqueueing lets say jquery UI scripts then make sure you have wp_footer(); in your footer.php file.

footer.php

<?php 
//Footer scripts
wp_footer(); 
?>
</body>
</html>
查看更多
登录 后发表回答