【问题标题】:wp_enqueue_script not displaying scriptwp_enqueue_script 不显示脚本
【发布时间】:2013-04-11 03:40:31
【问题描述】:

这是我第一次将 wp_enqueue_script 用于我试图在 WordPress 网站上设置的 IDX 解决方案。我的文件路径是 child-theme/js/idx.js

我把这段代码放在了我的functions.php文件中:

<?php
function wptuts_scripts_with_jquery()  
{  

    // Register the script like this for a theme:  
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/idx.js',      array( 'jquery' ) );  

// For either a plugin or a theme, you can then enqueue the script:  
    wp_enqueue_script( 'custom-script' );  
}  
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );  ?>

我的 idx.js 文件中的这段代码:

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript" ></script>
<script src="http://tools.example.com/scripts/postmessage.min.js" type="text/javascript" ></script>
<script type="text/javascript" >
$.createDynamicFrame('http://example.example.com', 'http://example.example.com/26611/Carets/search?parentUrl=' + encodeURIComponent( document.location.href ), 'MapSearch');

这无济于事。当我将 functions.php 中的 wptuts_scripts_with_jquery() 函数与其他脚本一起使用时,它似乎可以工作,但不能与 idx.js 脚本一起使用。任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: javascript wordpress


    【解决方案1】:

    我不知道您为什么要尝试在外部资源中包含 &lt;script&gt; 标记,但这一切都错了——它们应该只包含纯 javascript(这是浏览器已经期待的)。

    您的idx.js 文件应仅包含以下行:$.createDynamicFrame(...


    关于为什么不包含这个错误文件的原因 - 在 Wordpress 中排队脚本时作为参数传递的数组是依赖项列表。我假设既然你试图在你的 JS 文件中包含 jQuery - 那么它之前没有被模板加载过?在这种情况下,因为您还没有排队 jQuery - “自定义脚本”没有所有必需的组件,因此它不会被推送到页面上。

    默认情况下,wordpress 已经注册了一个 jQuery 版本 - 如果出于 CDN 偏好(或向后兼容的缘故)您需要使用不同的版本,那么您应该 wp_deregister_script 并将其重新加入您想要的版本。否则,您的函数应如下所示:

    function wptuts_scripts_with_jquery(){
      $path = get_template_directory_uri();
      wp_enqueue_script('jquery');
      wp_enqueue_script('custom-script', "{$path}/js/idx.js", array('jquery'));  
    } 
    

    由于tools.example.com/scripts/postmessage.min.js 不存在任何内容,因此我将其省略了。另请注意,您可以在单个 wp_enqueue_script 语句中注册和入队。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2016-07-13
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多