現在、WordPressを使ったサイトで、アーカイブページをajaxを使って無限ロードできるようにしようとしています。
その際、ajaxで取得したデータ(配列)が上手く取り出せずにいます。
その配列構造は以下です。
javascript
1{ 2"noDataFlg" : "1か0が入ります", 3"html" : "取得したデータが入ります" 4}
調べてみると、[]内にキーを入れれば取り出せるとかいてあったので
これを以下のようにして試しましたが、undefinedとなってしいます。
javascript
1console.log(data["html"]);
他に連想配列でキーから値を取り出す方法はないでしょうか?
javascript
1 2<script> 3//Ajax 4$(function(){ 5 var now_post_num = 5; 6 var get_post_num = 5; 7 $('#moredisp').on('click', function(){ 8 //$("#moredisp").html(ロード画像); 9 $.ajax({ 10 url: 'http://test.localhost/wordpress/wp-content/themes/news-mix-lite/ajax-load.php', 11 type: 'post', 12 data:{ 13 'now_post_num': now_post_num, 14 'get_post_num': get_post_num 15 }, 16 success: function(data){ 17 console.log(data); 18 now_post_num = now_post_num + get_post_num; 19 $("#content").append(data["html"]); 20 $("#moredisp").remove(); 21 if(!data["noDataFlg"]){ 22 $("#content").append('<a id="moredisp" href="#">もっと見る</a>'); 23 } 24 } 25 }); 26 return false; 27 }); 28}); 29</script>
php
1<?php 2require_once("../../../wp-config.php"); 3 4$now_post_num = $_POST['now_post_num']; 5$get_post_num = $_POST['get_post_num']; 6 7$next_now_post_num = $now_post_num + $get_post_num; 8$next_get_post_num = $get_post_num + $get_post_num; 9 10$sql = "SELECT 11 $wpdb->posts.ID, 12 $wpdb->posts.post_title, 13 $wpdb->posts.post_content 14 FROM 15 $wpdb->posts 16 WHERE 17 $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' 18 ORDER BY 19 $wpdb->posts.post_date DESC 20 LIMIT $now_post_num, $get_post_num"; 21 22$results = $wpdb->get_results($sql); 23 24$sql = "SELECT 25 $wpdb->posts.ID, 26 $wpdb->posts.post_title, 27 $wpdb->posts.post_content 28 FROM 29 $wpdb->posts 30 WHERE 31 $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' 32 ORDER BY 33 $wpdb->posts.post_date DESC 34 LIMIT $next_now_post_num, $next_get_post_num"; 35 36$next_results = $wpdb->get_results($sql); 37 38$noDataFlg = 0; 39if ( count($results) < $get_post_num || !count($next_results) ) { 40 $noDataFlg = 1; 41} 42 43$html = ""; 44 45foreach($results as $result){ 46 $html .= '<li>'; 47 $html .= '<p><a href="'.get_permalink($result->ID).'">'.apply_filters('the_title', $result->post_title).'</a></p>'; 48 $html .= '<p>'.apply_filters('the_content', $result->post_content).'</p>'; 49 $html .= '</li>'; 50} 51 52$returnObj = array(); 53$returnObj = array( 54 'noDataFlg' => $noDataFlg, 55 'html' => $html, 56); 57$returnObj = json_encode($returnObj); 58 59echo $returnObj; 60?>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/24 06:42
2017/02/24 06:59