Skip to content

Instantly share code, notes, and snippets.

@tanshio
Last active August 31, 2017 14:18
Show Gist options
  • Save tanshio/4a4a9c1b7bb2db03049d8134029f2d99 to your computer and use it in GitHub Desktop.
Save tanshio/4a4a9c1b7bb2db03049d8134029f2d99 to your computer and use it in GitHub Desktop.
20170624
<?php
/*
Template Name: インクリメンタルサーチページ
*/
?>
<?php get_header(); ?>
<h1>インクリメンタルサーチ</h1>
<input type="text" id="js-search">
<ul id="js-list">
</ul>
<script>
jQuery('#js-search').on('keyup', function (e) {
var $list = jQuery('#js-list');
var text = jQuery(this).val();
if (text === '') {
$list.empty();
return;
}
jQuery.ajax({
url:'/wp-json/wp/v2/posts?_embed&search='+text,
type:'get'
})
.done(function(data){
console.log(data);//データのリスト
$list.empty();
data.forEach(function(item){
$list.append('<li><a href="+item.title.link+">'+item.title.rendered+'</a></li>')
})
})
.fail(function(data){
console.log(data);
});
})
</script>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment