将Jquery与wordpress主题一起使用


Using Jquery with wordpress theme?

我目前正在使用wordpress 3.4.1我想把jquery嵌入我自己的wordpress主题中,但我做不到。尝试了网络上可用的最大可能的解决方案,但仍然不能令人信服地实现它。请帮助这个简单的例子!!

为了获得最佳兼容性,请使用WordPress附带的jQuery版本(加载特定版本的jQuery可能会在未来破坏您的插件):

添加到功能.php:

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

或者,如果你想从谷歌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');

点击此处阅读更多信息:http://codex.wordpress.org/Function_Reference/wp_enqueue_script

也可以使用以下链接:

http://code.jquery.com/jquery-latest.min.js-最新版本

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js-1.x系列中的最新版本

http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js-最新版本1.7.x系列

http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js-版本1.8.x系列中的最新版本

try下面的代码。

下面的代码在您的主题中添加了您的函数.php

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>
<?    
}

以下代码添加在主题header.php文件中的html头标记之间

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