【问题标题】:loading jQuery/JavaScript in functions.php (wordpress)在functions.php(wordpress)中加载jQuery/JavaScript
【发布时间】:2014-06-05 21:38:43
【问题描述】:

我正在尝试将谷歌地图功能加载到我的 wordpress 主题的 functions.php 文件中。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&extension=.js"></script>    
<script type="text/javascript" >
< !-- updatepage(); //-->< /script >

< /script >

主题的创建者告诉我使用此 .php 文件中的 wp_enqueue_script() 函数来调用它。

我的问题是,当我尝试调用它时,&lt; 会导致问题...显然我不知道如何以正确的格式调用它... .我怎样才能正确插入它,以便我的全屏谷歌地图正常工作?

【问题讨论】:

  • wp_enqueue_script('http://maps.googleapis.com/maps/api/js?sensor=false&amp;extension=.js', array('jquery'));
  • 当您不确定某件事的工作原理时,请阅读文档。 Wordpress 有一个完整的代码库,其中包含几乎每个功能如何工作的示例。
  • 太棒了。我会试试看它是否有效。丹克!
  • @user3537256 向我们展示您当前的 php 代码。
  • adeneo:我尝试使用上面的 wp 编码.....不知道我做错了什么,或者我的原始问题中我可以取出什么...更新页面,输入=text.....我不断收到错误消息

标签: javascript php jquery wordpress google-maps-api-3


【解决方案1】:

您应该为要运行的 jQuery 代码创建一个 .js 文件。

出于本示例的目的,我将此文件命名为customjquery.js。您可以随意命名,但请务必编辑下面的代码以反映新的文件名。

1] 创建一个customjquery.js 文件:

(function($) {
  updatepage();
})(jQuery);

2]然后你需要把customjquery.js文件放到你主题的/js/目录下。

3]将此代码添加到您的主题functions.php 文件中:

function my_custom_scripts() {
    wp_enqueue_script('jquery');
    wp_enqueue_script('google-maps-api', '//maps.googleapis.com/maps/api/js?sensor=false&extension=.js', array('jquery'));
    wp_enqueue_script('my-custom-jquery', get_template_directory_uri() . '/js/customjquery.js', array('jquery'));
}
add_action('wp_enqueue_scripts', 'my_custom_scripts');

如果您想将 customjquery.js 文件放在主题文件夹中的其他位置,只需更新上面代码中的路径以反映文件的新位置。

【讨论】:

    【解决方案2】:

    如果您只想将它​​包含在您的网页中,那么您可以使用适当的 add_action() 来回显它。但是,如果你想用你的 functions.php 运行它,那么我建议将代码转换为 php 或 wp_enqueue_script()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多