実現したいこと
WordPressで閲覧履歴のリストを吐き出すにあたって、[posts_per_page]が効かない問題に遭遇致しました。
後述するコードは、<?php readpost_typecheack(100); ?>で閲覧履歴を100件出力し、それを15件ずつに分割してページネーションを付ける。という機能を実現せんとするものです。
しかしながら、100件の出力がされてしまい、[posts_per_page]に書いた15件という指定が効きません。
該当のソースコード
php
1/* 履歴の登録 */ 2add_action( 'get_header', 'readpost'); 3 4function readpost() { 5 global $browsing_histories; 6 $browsing_histories = null; 7 $set_this_ID = null; 8 if( is_single() ){ 9 if( isset($_COOKIE['postid_history']) ){ 10 //cookieの値を呼び出し 11 $browsing_histories = explode(",", $_COOKIE['postid_history']); 12 if( $browsing_histories[0] != get_the_ID() ){ 13 if( count($browsing_histories) >= 50 ){ 14 $set_browsing_histories = array_slice($browsing_histories , 0, 49); 15 }else{ 16 $set_browsing_histories = $browsing_histories; 17 } 18 //値の先頭が現在の記事IDでなければ文字列の一番最初に追加 19 $set_this_ID = get_the_ID().','.implode(",", $set_browsing_histories); 20 setcookie( 'postid_history', $set_this_ID, time() + 60 * 60 * 24 * 365 * 1,'/' ); 21 // }else{ 22 // $set_this_ID = $_COOKIE['postid_history']; 23 } 24 }else{ 25 //cookieがなければ、現在の記事IDを保存 26 $set_this_ID = get_the_ID(); 27 setcookie( 'postid_history', $set_this_ID, time() + 60 * 60 * 24 * 365 * 1,'/' ); 28 } 29 //呼び出しのみ 30 }else{ 31 if( isset($_COOKIE['postid_history']) ){ 32 $browsing_histories = explode(",", $_COOKIE['postid_history']); 33 } 34 } 35 $postread = explode( ",", $_COOKIE['postid_history'] ); 36 $postread = array_unique( $postread ); 37 $postread = array_values( $postread ); 38 return $postread; 39} 40 41/* 履歴の出力 */ 42function readpost_typecheack( $postnum ) { 43 $postdate = readpost(); 44 $numlist = 0; 45 if( !empty($postdate) ): 46 ?> 47 <ul> 48 <?php 49 // setup_postdata利用のためのglobal $post; 50 global $post; 51 52 // 出力条件を$posts_arrayにセット 53 $args = array( 54 'posts_per_page' => 15, // 効かない! 55 'include' => $postdate, 56 'post_type' => 'mypost' 57 ); 58 $posts_array = get_posts( $args ); 59 60 // $posts_arrayをsetup_postdataにセット 61 foreach ( $posts_array as $post ) : setup_postdata( $post ); 62 63 // $postnum件で出力 64 if($postnum==$numlist){ break; } 65 ?> 66 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 67 <?php $numlist++; 68 endforeach; 69 wp_reset_postdata();?> 70 </ul> 71 <?php endif; 72}
対処法について何かアドバイス頂戴できましたら幸甚に存じます。
何卒宜しくお願い申し上げます。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/28 07:34