WordPressにて以下のことを行いたいと考えております。
・特定ページにブログ記事を最新記事5件取得して表示(タイトルと本文のみ)
・5件表示したあとは、ページ送りをする
・前ページと次ページボタンを設けて前の5件次の5件を取得できるようにしたいです
・無料のテーマLightningを使用しており、子テーマのfunctions.phpに以下を記載しました。
質問==========
このショートコード自体は動くのですが、
5件取得後のページ送りボタンで、次または前の5件を取得する機能をどのように実装したら良いのかわからない状態です。
php
1// 一覧記事取得関数 -------------------------------------------------------------------------------- 2// "num" = 表示記事数, 3 4function getCatItems($atts, $content = null) { 5 extract(shortcode_atts(array( 6 "num" => '5' 7 8 ), $atts)); 9 10 // 処理中のpost変数をoldpost変数に退避 11 global $post; 12 $oldpost = $post; 13 14 15 $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date'); 16 17 if($myposts) { 18 // 記事がある場合↓ 19 $retHtml = '<div class="getPostDispArea">'; 20 // 取得した記事の個数分繰り返す 21 foreach($myposts as $post) : 22 // 投稿ごとの区切りのdiv 23 $retHtml .= '<div class="getPost">'; 24 25 // 記事オブジェクトの整形 26 setup_postdata($post); 27 28 29 // 文章のみのエリアをdivで囲う 30 $retHtml .= '<div class="getPostStringArea">'; 31 32 // 投稿年月日を取得 33 $year = get_the_time('Y'); // 年 34 $month = get_the_time('n'); // 月 35 $day = get_the_time('j'); // 日 36 37 $retHtml .= '<span>この記事は' . $year . '年' . $month . '月' . $day . '日に投稿されました</span>'; 38 39 // タイトル設定(リンクも設定する) 40 41 // 本文を抜粋して取得 42 $getString = get_the_content(); 43 $retHtml.= '<div class="getPostContent">' . $getString . '</div>'; 44 45 $retHtml.= '</div></div>'; 46 47 endforeach; 48 49 $retHtml.= '</div>'; 50 51 52 } else { 53 // 記事がない場合↓ 54 $retHtml='<p>記事がありません。</p>'; 55 } 56 57 // oldpost変数をpost変数に戻す 58 $post = $oldpost; 59 60 return $retHtml; 61} 62// 呼び出しの指定 63add_shortcode("getCategoryArticle", "getCatItems");
あなたの回答
tips
プレビュー