同じカテゴリーで前ページへ、次ページへのリンクを付けたい。
つまり、1つのカテゴリーを連続した読み物?にする場合だよね?
基本的には、記事下にこう書けばいい(プラグイン Exec-PHP は、必須)
<a href="<?php $prev_post = get_previous_post(true); ?>">Prev</a> <a href="<?php $prev_post = get_next_post(true); ?>">Next</a>
もちろん、上記のようなものでプラグインも出ている。
でも、ほとんどの場合、前述の仕様では要求を満足できない。
同じカテゴリーを1から順番に・・・という仕様がほとんどだと思う。というか、そういう場面にしか遭遇しないw
以下を記事下に埋め込めばいい。cat=5 の部分は、カテゴリーIDをセットしてあげる
<br /> <div align="center"> <?php query_posts('cat=5&order=ASC'); if (have_posts()) { global $post; $my_postID =$post->ID; $tops_poxt = NULL; $next_poxt = NULL; $prev_poxt = NULL; $botm_poxt = NULL; $getnx=0; $nxID=NULL; while (have_posts()){ the_post(); $botm_poxt=$post->ID; if( empty( $tops_poxt ) ){ $tops_poxt=$post->ID; } if( $post->ID==$my_postID ){ $prev_poxt = $nxID; $getnx=1; }elseif($getnx==1){ $getnx=0; $next_poxt = $post->ID; }else{ $nxID=$post->ID; } } } echo '|<<a href="'.get_permalink( $tops_poxt ).'">先頭へ</a> '; echo ' | '; if (!empty( $prev_poxt )){ $title=get_the_title( $prev_poxt ); echo '<<<a href="'.get_permalink( $prev_poxt ).'">'.$title.'</a>'; } if (!empty( $prev_poxt ) && !empty( $next_poxt )){ echo ' | '; } if (!empty( $next_poxt )){ $title=get_the_title( $next_poxt ); echo '<a href="'.get_permalink( $next_post ).'">'.$title.'</a>>>'; } echo ' | '; echo '<a href="'.get_permalink( $botm_poxt ).'">最後へ</a>>|'; ?> </div>
もちろんこれは、プラグインやウィジェットにしてしまってもいい。
長いからね、投稿のたびに張り付けるのもスマートじゃないし・・・ただ、カテゴリーIDをどうするか。
ちなみに、記事のカテゴリー id は、
$category_id = get_cat_ID('Category Name'); または $post_cat=get_the_category(); $cat_id=$post_cat[0]->cat_ID; 現在のカテゴリー情報を取得してIDを出力 $post_cat=get_the_category(); $cat=$post_cat[0];で、取得できる。
カテゴリーは、複数個設定できるという点も考慮しないといけないですが。
function.php にこれをたして。。
function prevnext_view($viewcat) { $query = 'cat='.$viewcat.'&order=ASC'; query_posts($query); if (have_posts()) { global $post; $my_postID =$post->ID; $tops_poxt = NULL; $next_poxt = NULL; $prev_poxt = NULL; $botm_poxt = NULL; $getnx=0; $nxID=NULL; while (have_posts()){ the_post(); $botm_poxt=$post->ID; if( empty( $tops_poxt ) ){ $tops_poxt=$post->ID; } if( $post->ID==$my_postID ){ $prev_poxt = $nxID; $getnx=1; }elseif($getnx==1){ $getnx=0; $next_poxt = $post->ID; }else{ $nxID=$post->ID; } } } echo '|<<a href="'.get_permalink( $tops_poxt ).'">先頭へ</a> '; echo ' | '; if (!empty( $prev_poxt )){ $title=get_the_title( $prev_poxt ); echo '<<<a href="'.get_permalink( $prev_poxt ).'">'.$title.'</a>'; } if (!empty( $prev_poxt ) && !empty( $next_poxt )){ echo ' | '; } if (!empty( $next_poxt )){ $title=get_the_title( $next_poxt ); echo '<a href="'.get_permalink( $next_post ).'">'.$title.'</a>>>'; } echo ' | '; echo '<a href="'.get_permalink( $botm_poxt ).'">最後へ</a>>|'; }記事下に
<?php prevnext_view(5); ?>でもいいわけですね?
スポンサーリンク