2015-04-26 67 views
1

我正在使用下面的方法在WordPress管理中使用jQuery库,这工作正常,我不需要为此方法添加jQuery库的源代码。如何将WordPress jQuery库添加到前端

<script> 
     jQuery(document).ready(function(){ 
      // works inside WordPress admin without source 
     }); 
    </script> 

但是,同样的方法不适用于前端WordPress窗体,并使其工作我要添加库的源?

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script> 
     jQuery(document).ready(function(){ 
      // need to add jQuery source 
     }); 
    </script> 

为什么我需要为前端添加jQuery库的源代码?如何将WordPress JQuery库加载到前端?

回答

1

它在管理员工作,因为有管理脚本有jQuery作为依赖和WordPress包括jQuery为了这些脚本工作。

添加脚本(不包括它们在脚本标记中)时,应该使用wp_enqueue_script()函数。注意该函数的变量$deps,如果将其设置为array('jquery'),WordPress将为您包含该库,以便您的脚本可以使用它。

If you scroll down the page您可以看到哪些库包含在WordPress中,以及使用哪些句柄对它们进行排队。

0

您首先需要确保jQuery已入队。它应该只是以确保它是添加下面的header.php中

<?php 
function my_jq_queue() { 
    wp_enqueue_script('jquery'); 
} 

add_action('wp_enqueue_scripts', 'my_jq_queue'); 
?> 

后,您可以添加脚本如下

<script type="text/javascript"> 
jQuery(document).ready(function($) { 
     // add add your function in the format $() 

    }); 
</script> 
+0

我已经在插件添加此。我需要将其添加到插件?是添加在主插件文件中? –

+0

php函数应该作为主插件文件中的单独函数运行。你可以添加JavaScript内联或也与链接到JavaScript文件排队它看到链接[http://code.tutsplus.com/articles/the-ins-and-outs-of-the-enqueue-script-for -wordpress主题-和插件 - WP-22509(http://code.tutsplus.com/articles/the-ins-and-outs-of-the-enqueue-script-for-wordpress-themes-and -plugins - wp-22509) –

+0

请检查我的这个问题http://stackoverflow.com/questions/29983563/add-wordpress-jquery-to-cutom-page-template –