Skip to content

Instantly share code, notes, and snippets.

@tanshio
Created June 23, 2017 23:48
Show Gist options
  • Save tanshio/021920dade446ebec395eac0b5aec960 to your computer and use it in GitHub Desktop.
Save tanshio/021920dade446ebec395eac0b5aec960 to your computer and use it in GitHub Desktop.
list
<?php
/*
Template Name: リストページ
*/
?>
<?php get_header(); ?>
<h1>追加リスト</h1>
<button id="js-add">リストの追加</button>
<ul id="js-list">
</ul>
<script>
var $button = jQuery('#js-add');
var $list = jQuery('#js-list');
var per_page = 1;
var count = 0;
$button.on('click', function (e) {
count++;
jQuery.ajax({
url:'/wp-json/wp/v2/posts?_embed&per_page='+per_page+'&page='+count,
type:'get'
})
.done(function(data){
console.log(data.length);//データのリスト
data.forEach(function(item){
$list.append('<li><a href="+item.title.link+">'+item.title.rendered+'</a></li>')
})
})
.fail(function(data){
console.log(data);
$button.prop('disabled',true);//上限以上になったらボタンを押させない
});
})
</script>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment