簡體   English   中英

將Jquery與wordpress主題一起使用?

[英]Using Jquery with wordpress theme?

我目前正在使用wordpress 3.4.1,我想將jquery嵌入我自己的wordpress主題中,但我無法做到這一點。 在網絡上嘗試了最大可能的解決方案,但仍無法令人信服地實現它。 請幫助簡單的例子!

嘗試下面的代碼。

下面的代碼在主題中添加了您的function.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 head標簽之間添加

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

為了獲得最佳兼容性,請使用WordPress隨附的jQuery版本(加載特定版本的jQuery將來可能會破壞您的插件):

添加到function.php:

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

或者,如果您想從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');

在這里閱讀更多: 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中的最新版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM