У меня проблема с Javascript, который я создал для своего пользовательского виджета.
Вот мой код из javascript к тому, как я называю его в моем виджете.
Custom.js
var wrapper = $('#wrapper');
$.ajax({
method: "POST",
url: "https://api.graphcms.com/simple/v1/SampleAPI",
contentType: "application/json",
headers: {
Authorization: "bearer ******"
},
data: JSON.stringify({query: "query { allProducts { id title } }"})
}).done(function(data) {
for( var key in data ) {
for (var i = 0; i<data[key].allProducts.length; i++)
{
console.log(data[key].allProducts[i]);
container = $('<div id="data" class="container"></div>');
wrapper.appendTo(container);
container.appendTo('<input type=checkbox name="id" value=' +
data[key].allProducts[i].id + '>' + data[key].allProducts[i].title );
}
}
functions.php
Вот как я вызываю Jquery в моем файле functions.php для шаблона:
function jquery_import() {
wp_deregister_script('jquery');
wp_register_script('jquery',https://ajax.googleapis.com/ajax/libs/3.3.1/jquery.min.js', array(), '3.3.1', true);
wp_enqueue_script('jquery');
}
add_action( 'wp_enqueue_scripts', 'jquery_import' );
Вот как я называю свой собственный файл JavaScript:
function custom_wp_enqueue_scripts() {
wp_register_script( 'custom', get_template_directory_uri() . '/assets/js/custom.js', array( 'jquery' ), NULL, false );
wp_enqueue_script( 'custom' );
}
add_action('wp_enqueue_scripts', 'custom_wp_enqueue_scripts');
Мой пользовательский виджет
Наконец, в моем виджете есть следующее выражение div:
<div id="wrapper"> </div>
На основании моего javascript поступают данные, однако информация не отображается в моем виджете. Почему это не отображается в моем виджете?
Спасибо,
Кевин Дэвис