質問編集履歴

1

ご回答を受けてコードを修正致しました。

2018/11/30 10:04

投稿

makirons
makirons

スコア20

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
  このうち、15件ずつの分割についてはできているものの、「★ページネーション表示」と「★ページネーション出力関数」がうまくいかないという状況でございます。
12
12
 
13
+
14
+
15
+
16
+
13
17
  ###該当のソースコード
14
18
 
15
19
 
@@ -102,210 +106,198 @@
102
106
 
103
107
  /**
104
108
 
105
- * 履歴の出力
109
+ * 履歴の出力(ご回答を受けてコードを修正致しました。)
106
110
 
107
111
  */
108
112
 
109
113
  function readpost_typecheack( $postnum ) {
110
114
 
111
- $postdate = readpost();
112
-
113
- $numlist = 0;
114
-
115
- if( !empty($postdate) ):
116
-
117
- ?>
118
-
119
- <ul>
120
-
121
- <?php
122
-
123
- // setup_postdata利用のためのglobal $post;
124
-
125
- global $post;
126
-
127
-
128
-
129
- // 出力条件を$posts_arrayにセット
130
-
131
- $args = array(
132
-
133
- 'paged' => $paged, //現在のページ番号
134
-
135
- 'posts_per_page' => 15,
136
-
137
- 'post__in' => $postdate,
138
-
139
- 'post_type' => 'mypost'
140
-
141
- );
142
-
143
- $posts_array = get_posts( $args );
144
-
145
-
146
-
147
- // $posts_arrayをsetup_postdataにセット
148
-
149
- foreach ( $posts_array as $post ) : setup_postdata( $post );
150
-
151
-
152
-
153
- // $postnum件で出力
154
-
155
- if($postnum==$numlist){ break; }
156
-
157
- ?>
158
-
159
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
160
-
161
- <?php $numlist++;
162
-
163
- endforeach;
164
-
165
-
166
-
167
- // ★ページネーション表示
168
-
169
- if ( function_exists( 'pagination' ) ) :
170
-
171
- pagination( $posts_array->max_num_pages, $paged );
172
-
173
- endif;
174
-
175
-
176
-
177
- wp_reset_postdata();?>
178
-
179
- </ul>
180
-
181
- <?php endif;
115
+ $postdate = readpost();
116
+
117
+ $numlist = 0;
118
+
119
+ if( !empty($postdate) ): ?>
120
+
121
+ <ul>
122
+
123
+ <?php
124
+
125
+ $paged = get_query_var('paged') ?: 1;
126
+
127
+ $args = array(
128
+
129
+ 'paged' => $paged,
130
+
131
+ 'posts_per_page' => 15,
132
+
133
+ 'post__in' => $postdate,
134
+
135
+ 'post_type' => 'mypost'
136
+
137
+ );
138
+
139
+ $the_query = new WP_Query( $args );
140
+
141
+
142
+
143
+ if($postnum==$numlist){ break; }
144
+
145
+
146
+
147
+ if ( $the_query->have_posts() ) :
148
+
149
+ while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
150
+
151
+ <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
152
+
153
+ <?php endwhile;
154
+
155
+ endif;
156
+
157
+
158
+
159
+ /* ページネーション表示 */
160
+
161
+ if ( function_exists( 'pagination' ) ) :
162
+
163
+ pagination( $the_query->max_num_pages, $paged );
164
+
165
+ endif;
166
+
167
+
168
+
169
+ wp_reset_postdata();?>
170
+
171
+ </ul>
172
+
173
+ <?php endif;
174
+
175
+ }
176
+
177
+
178
+
179
+ /**
180
+
181
+ * ★ページネーション出力関数
182
+
183
+ * $paged : 現在のページ
184
+
185
+ * $pages : 全ページ数
186
+
187
+ * $range : 左右に何ページ表示するか
188
+
189
+ * $show_only : 1ページしかない時に表示するかどうか
190
+
191
+ */
192
+
193
+ function pagination( $pages, $paged, $range = 2, $show_only = false ) {
194
+
195
+
196
+
197
+ $pages = ( int ) $pages; //float型で渡ってくるので明示的に int型 へ
198
+
199
+ $paged = $paged ?: 1; //get_query_var('paged')をそのまま投げても大丈夫なように
200
+
201
+
202
+
203
+ //表示テキスト
204
+
205
+ $text_first = "« 最初へ";
206
+
207
+ $text_before = "‹ 前へ";
208
+
209
+ $text_next = "次へ ›";
210
+
211
+ $text_last = "最後へ »";
212
+
213
+
214
+
215
+ if ( $show_only && $pages === 1 ) {
216
+
217
+ // 1ページのみで表示設定が true の時
218
+
219
+ echo '<div class="pagination"><span class="current pager">1</span></div>';
220
+
221
+ return;
222
+
223
+ }
224
+
225
+
226
+
227
+ if ( $pages === 1 ) return; // 1ページのみで表示設定もない場合
228
+
229
+
230
+
231
+ if ( 1 !== $pages ) {
232
+
233
+ //2ページ以上の時
234
+
235
+ echo '<div class="pagination"><span class="page_num">Page ', $paged ,' of ', $pages ,'</span>';
236
+
237
+ if ( $paged > $range + 1 ) {
238
+
239
+ // 「最初へ」 の表示
240
+
241
+ echo '<a href="', get_pagenum_link(1) ,'" class="first">', $text_first ,'</a>';
242
+
243
+ }
244
+
245
+ if ( $paged > 1 ) {
246
+
247
+ // 「前へ」 の表示
248
+
249
+ echo '<a href="', get_pagenum_link( $paged - 1 ) ,'" class="prev">', $text_before ,'</a>';
250
+
251
+ }
252
+
253
+ for ( $i = 1; $i <= $pages; $i++ ) {
254
+
255
+
256
+
257
+ if ( $i <= $paged + $range && $i >= $paged - $range ) {
258
+
259
+ // $paged +- $range 以内であればページ番号を出力
260
+
261
+ if ( $paged === $i ) {
262
+
263
+ echo '<span class="current pager">', $i ,'</span>';
264
+
265
+ } else {
266
+
267
+ echo '<a href="', get_pagenum_link( $i ) ,'" class="pager">', $i ,'</a>';
268
+
269
+ }
270
+
271
+ }
272
+
273
+
274
+
275
+ }
276
+
277
+ if ( $paged < $pages ) {
278
+
279
+ // 「次へ」 の表示
280
+
281
+ echo '<a href="', get_pagenum_link( $paged + 1 ) ,'" class="next">', $text_next ,'</a>';
282
+
283
+ }
284
+
285
+ if ( $paged + $range < $pages ) {
286
+
287
+ // 「最後へ」 の表示
288
+
289
+ echo '<a href="', get_pagenum_link( $pages ) ,'" class="last">', $text_last ,'</a>';
290
+
291
+ }
292
+
293
+ echo '</div>';
294
+
295
+ }
182
296
 
183
297
  }
184
298
 
185
299
 
186
300
 
187
- /**
188
-
189
- * ★ページネーション出力関数
190
-
191
- * $paged : 現在のページ
192
-
193
- * $pages : 全ページ数
194
-
195
- * $range : 左右に何ページ表示するか
196
-
197
- * $show_only : 1ページしかない時に表示するかどうか
198
-
199
- */
200
-
201
- public static function pagination( $pages, $paged, $range = 2, $show_only = false ) {
202
-
203
-
204
-
205
- $pages = ( int ) $pages; //float型で渡ってくるので明示的に int型 へ
206
-
207
- $paged = $paged ?: 1; //get_query_var('paged')をそのまま投げても大丈夫なように
208
-
209
-
210
-
211
- //表示テキスト
212
-
213
- $text_first = "« 最初へ";
214
-
215
- $text_before = "‹ 前へ";
216
-
217
- $text_next = "次へ ›";
218
-
219
- $text_last = "最後へ »";
220
-
221
-
222
-
223
- if ( $show_only && $pages === 1 ) {
224
-
225
- // 1ページのみで表示設定が true の時
226
-
227
- echo '<div class="pagination"><span class="current pager">1</span></div>';
228
-
229
- return;
230
-
231
- }
232
-
233
-
234
-
235
- if ( $pages === 1 ) return; // 1ページのみで表示設定もない場合
236
-
237
-
238
-
239
- if ( 1 !== $pages ) {
240
-
241
- //2ページ以上の時
242
-
243
- echo '<div class="pagination"><span class="page_num">Page ', $paged ,' of ', $pages ,'</span>';
244
-
245
- if ( $paged > $range + 1 ) {
246
-
247
- // 「最初へ」 の表示
248
-
249
- echo '<a href="', get_pagenum_link(1) ,'" class="first">', $text_first ,'</a>';
250
-
251
- }
252
-
253
- if ( $paged > 1 ) {
254
-
255
- // 「前へ」 の表示
256
-
257
- echo '<a href="', get_pagenum_link( $paged - 1 ) ,'" class="prev">', $text_before ,'</a>';
258
-
259
- }
260
-
261
- for ( $i = 1; $i <= $pages; $i++ ) {
262
-
263
-
264
-
265
- if ( $i <= $paged + $range && $i >= $paged - $range ) {
266
-
267
- // $paged +- $range 以内であればページ番号を出力
268
-
269
- if ( $paged === $i ) {
270
-
271
- echo '<span class="current pager">', $i ,'</span>';
272
-
273
- } else {
274
-
275
- echo '<a href="', get_pagenum_link( $i ) ,'" class="pager">', $i ,'</a>';
276
-
277
- }
278
-
279
- }
280
-
281
-
282
-
283
- }
284
-
285
- if ( $paged < $pages ) {
286
-
287
- // 「次へ」 の表示
288
-
289
- echo '<a href="', get_pagenum_link( $paged + 1 ) ,'" class="next">', $text_next ,'</a>';
290
-
291
- }
292
-
293
- if ( $paged + $range < $pages ) {
294
-
295
- // 「最後へ」 の表示
296
-
297
- echo '<a href="', get_pagenum_link( $pages ) ,'" class="last">', $text_last ,'</a>';
298
-
299
- }
300
-
301
- echo '</div>';
302
-
303
- }
304
-
305
- }
306
-
307
-
308
-
309
301
  ```
310
302
 
311
303
  長いコードで誠に恐れ入りますが、対処法について何かアドバイス頂戴できましたら幸甚に存じます。