Last active
August 31, 2017 14:18
-
-
Save tanshio/4a4a9c1b7bb2db03049d8134029f2d99 to your computer and use it in GitHub Desktop.
20170624
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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