
WordPress で「もっと見る」を実装(プラグイン無し)②
本記事にはアフィリエイト広告が含まれます。
前回の作業に追加しました。
目次
対応状況
対応済
- TOPページ
 - カテゴリアーカイブ
 - 検索結果(日本語結果が微妙な気がする、要検証)
 
未対応
- 月別アーカイブ
 
いずれ対応したい
- 自動スクロール
 
jQuery
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29  | 
						function moreDisp() {   // more   $(document).on("click","#more_disp a", function() {     var now_post_num = $('.large-8.medium-8.columns .card.callout').length;      var get_post_num = 10; // 一度に取得する数      var dir = location.href.split("/");     var get_post_str = dir[dir.length -1];     var more_disp = $("#more_disp");         $.ajax({             type: 'post',             url: '/PATH/more-disp.php',             data: {                 'now_post_num': now_post_num,                 'get_post_num': get_post_num,                 'get_post_str': get_post_str             },             success: function(data) {                 now_post_num = now_post_num + get_post_num;                 data = JSON.parse(data);                 $(".large-8.medium-8.columns").append(data['html']);                 $(more_disp).remove();                 if (!data['noDataFlg']) {                     $(".large-8.medium-8.columns").append(more_disp);                 }             }         });         return false;     }); }  | 
					
PHP
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85  | 
						<?php require_once("/WP PATH/wp-config.php"); $now_post_num = $_POST['now_post_num']; $get_post_num = $_POST['get_post_num']+1; $get_post_str = $_POST['get_post_str']; if (empty($get_post_str)){     $results = $wpdb->get_results($wpdb->prepare("         SELECT             $wpdb->posts.ID,             $wpdb->posts.post_author,             $wpdb->posts.post_title,             $wpdb->posts.post_content         FROM $wpdb->posts         WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish'          ORDER BY              $wpdb->posts.post_date DESC          LIMIT $now_post_num, $get_post_num         ")); } elseif (preg_match('/^\?s=/',$get_post_str)) {     $keyword = str_replace('?s=','', $get_post_str);     $keyword = urldecode($keyword);     $keyword = '%'.like_escape( $keyword ).'%';     $results = $wpdb->get_results($wpdb->prepare("         SELECT             $wpdb->posts.ID,             $wpdb->posts.post_author,             $wpdb->posts.post_title,             $wpdb->posts.post_content         FROM $wpdb->posts         WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_title LIKE %s         ORDER BY              $wpdb->posts.post_date DESC          LIMIT $now_post_num, $get_post_num         ",$keyword)); } else {     $results = $wpdb->get_results($wpdb->prepare("         SELECT             $wpdb->posts.ID,             $wpdb->posts.post_author,             $wpdb->posts.post_title,             $wpdb->posts.post_content,             $wpdb->terms.name         FROM $wpdb->posts         JOIN $wpdb->term_relationships ON $wpdb->term_relationships.object_id = $wpdb->posts.ID         JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id         JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id         WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' AND $wpdb->terms.slug = '$get_post_str'         ORDER BY              $wpdb->posts.post_date DESC          LIMIT $now_post_num, $get_post_num         ")); } if ( count($results) < $get_post_num ) {     $noDataFlg = 1; } else {     $noDataFlg = 0;     array_pop($results); } $html = ""; foreach ($results as $value) {     $html .= '<div class="card callout">';     $html .= '<div class="row">';     $html .= '<div class="large-3 medium-3 small-3 columns"><p>'.get_the_post_thumbnail($value->ID).'</p></div>';     $html .= '<h5><a href="'.get_permalink($value->ID).'">'.apply_filters('the_title', $value->post_title).'</a></h5>';     $html .= '<p class="excerpt">'.mb_substr(strip_tags($value-> post_content),0,100).'...</p>';     $html .= '<p class="posted text-right">'.' '.get_the_date( 'Y.m.d', $value->ID ).' '.get_the_author_meta('display_name', $value->post_author ).' '.get_avatar($value->post_author,35).'</p>';     $html .= '</div>';     $html .= '</div>';     $html .= '</div>'; } $returnObj = array(); $returnObj = array(     'noDataFlg' => $noDataFlg,     'html' => $html, ); $returnObj = json_encode($returnObj); echo $returnObj;  ?>  | 
					
もっと簡潔に記述したいものだけど…
PHP7.0にアップデート 中に気付いたけど、WPのデバッグモードがONになっていると動作しない。
            お母ちゃん 
             2017.6.27