我如何将这个脚本插入wordpress?

时间:2014-07-01 09:18:24

标签: jquery wordpress

所以在iTunes中作为app的朋友,让我成为一个脚本,在网站中使用它的一部分。他没有使用wordpress,并且让我想出来,但是不能让它真正起作用。什么是最好的方法?他给了我一个.htm文件,里面的代码看起来像这样:

<!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 个答案:

答案 0 :(得分:1)

您可以使用wp_enqueue_script()将JavaScript排入队列。创建一个名为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