2013-04-10 87 views
0

這是我第一次使用wp_enqueue_script作爲我嘗試在WordPress網站上設置的IDX解決方案。我的文件路徑是兒童主題/ JS/idx.jswp_enqueue_script不顯示腳本

我把這個代碼在我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腳本一起工作。任何幫助將非常感激。

謝謝!

回答

0

我不知道你爲什麼試圖在外部資源中包含<script>標籤,但這一切都是錯誤的 - 他們應該只包含純JavaScript(這是瀏覽器已經期待的)。

idx.js文件應該只包含這一行:$.createDynamicFrame(...


走上爲什麼沒有被列入,雖然這種錯誤文件的原因 - 你排隊WordPress的腳本時作爲參數傳遞的數組列表的依賴關係。我假設,因爲你試圖將jQuery包含在你的JS文件中 - 那麼它以前沒有被模板加載過?在這種情況下,,因爲您還沒有入隊 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聲明中註冊寄存器入隊