2014-07-01 2 views
-1

그래서 iTunes에서 앱으로있는 친구가 내게 웹 사이트에서 그 일부를 사용하는 스크립트를 만들었습니다. 그는 워드 프레스로 일을 나던, 그리고 나에게 알아 내려고 그것을 줬어,하지만 정말 작동하지 않습니다. 여기서 가장 좋은 접근 방법은 무엇입니까? 그는 나에게 .htm 파일을주고, 코드 안에 다음과 같습니다 : 당신은 자바 스크립트를 대기열에 wp_enqueue_script()을 사용어떻게이 스크립트를 wordpress에 넣을 수 있습니까?

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 

</head> 
<body> 
Skatespots 
<div id="spotsContainer"> 

</div> 

<script> 

(function() { 

// Localize jQuery variable 
var jQuery; 

/******** Load jQuery if not present *********/ 
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '10.0.1') { 
var script_tag = document.createElement('script'); 
script_tag.setAttribute("type","text/javascript"); 
script_tag.setAttribute("src","https://code.jquery.com/jquery-git1.min.js"); 
if (script_tag.readyState) { 
    script_tag.onreadystatechange = function() { // For old versions of IE 
     if (this.readyState == 'complete' || this.readyState == 'loaded') { 
      scriptLoadHandler(); 
     } 
    }; 
} else { 
    script_tag.onload = scriptLoadHandler; 
} 
// Try to find the head, otherwise default to the documentElement 
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); 
} else { 
    // The jQuery version on the window is the one we want to use 
jQuery = window.jQuery; 
main(); 
} 

/******** Called once jQuery has loaded ******/ 
function scriptLoadHandler() { 
// Restore $ and window.jQuery to their previous values and store the 
// new jQuery in our local jQuery variable 
jQuery = window.jQuery.noConflict(true); 
// Call our main function 
main(); 
} 

function main() { 
jQuery(document).ready(function($) { 

    $.getJSON("http://sk8spots.com/Core/GetData?userId=9914bd37-2526-4c0e-95ed-ad16b045ab89&callback=?", function(data) { 

var str = ""; 
$.each(data.Model, function (i, item) { 
      str += "<div class='item' style='height:200px;width:250px;float:left;'><img src='http://sk8spots.com/imageBank/" + item.Images[0].Id + ".jpg?width=100&height=100&bgcolor=white' width='100' height='100' /><p>" + item.Name + "</p><p>" + item.Description + "</p></div>"; 
    }); 
    $("#spotsContainer").append(str); 
console.log(data); 

    }); 
}); 
} 

})(); 

</script> 

답변

1

. script.js 파일을 만들어 테마 디렉토리의 폴더에 업로드하십시오. 예 : /my-theme/js/script.js

그런 다음 wp_enqueue_script 기능을 사용합니다. 따라서, 예를 들어 :

function my_enqueue_scripts() { 
    wp_enqueue_script('script-name', get_template_directory_uri() . '/js/script.js', array(), '1.0', true); 
} 

add_action('wp_enqueue_scripts', 'my_enqueue_scripts'); 

참조 : http://codex.wordpress.org/Function_Reference/wp_enqueue_script

+0

감사합니다. 캔트가 작동하도록하지만, 페이지를로드하고 보여 주지만 비어 있습니다. 예제 코드로 같은 결과가 나온다. – user2059370

+0

예제 코드를 어디에 넣고 있습니까? 그것은 당신의 주제 안에 functions.php 파일을 넣어야 할 것입니다. 여기에서보십시오 http://codex.wordpress.org/Functions_File_Explained – henrywright

+0

오, 내 편이라 사소한 오류. 그 지금 일하고있어. 고마워요! – user2059370

관련 문제