小説家になろうにて「キモオタでギャルゲー、それって何の罰ゲーム!?」連載中!

WordPressで個別記事のカテゴリ別ページ送りができない! ~テーマ別パターンも掲載

カスタマイズ&プラグイン
この記事は約18分で読めます。

質問者の写真

小説のページのはずなのに、次ページのナビが昨日食べたイチゴ大福の記事になっちゃってる……
 
解答者の写真
美味しかった?
 

 

質問者の写真

違うからっ! ちゃんと次も小説ページをナビしたいの!
 

(※本記事は以前に書いた記事を大幅に改訂したものです)

【2018.12.16】
Luxeritasバージョン3.0以上に対応

スポンサーリンク
この記事を読む方へのオススメ

カテゴリ別の個別記事ページ遷移

この問題、ハマっている方は多いと思います。

以前の私もそうでした。
なんせ、ここは小説サイト。
連載物を掲載するときは、同一カテゴリ(同じ小説)でページ送りできないと致命的ですから。
(完結していればnextpageで分割する手法もありますが)

デフォルトのページナビ設定だと、カテゴリを無視した全記事で遷移します。
だから前項の会話みたいになってしまうわけでして。
カテゴリごとにページ遷移させたいなら、テーマをカスタマイズする必要があります。

解答者の写真
だけど実は、テーマによってページナビの実装コードが違うの!
 

 

本稿では具体例としてSimplicityLuxeritasにおける方法を挙げます。
大抵のテーマは、どちらかの方法で片付くのではないかと思います。

スポンサーリンク

Simplicityの場合

Simplicityのサムネイルページ送りテンプレートはpager-post-navi-thumbnail.php
1行目から数行記します。

<div class="navigation">
<div id="prev-next" class="clearfix">
<?php
$prevpost = get_adjacent_post(false, '', true); //前の記事
$nextpost = get_adjacent_post(false, '', false); //次の記事
if( $prevpost or $nextpost ){ //前の記事、次の記事いずれか存在しているとき
?>

4~5行目のget_adjacent_post();のパラメーターを次の通りに修正します。

$prevpost = get_adjacent_post(true, '', true); //前の記事
$nextpost = get_adjacent_post(true, '', false); //次の記事

検索して見かけるページ送りのパターンの多くは、大体これに当てはまる気がします。

Luxeritas(V3.x以前)の場合

Simplicityと違って、ちょっと厄介です。

Luxeritasのページ送りは投稿テンプレート(single.php)に直書きされています。
この部分。

<?php
if( isset( $luxe['next_prev_nav_visible'] ) ) {
?>
<div id="pnavi" class="grid">
<?php 
 $next_post = get_next_post();
 if( $next_post ) {
 $next_thumb = get_the_post_thumbnail($next_post->ID, 'thumb100');
 if( empty( $next_thumb ) ) $next_thumb = '<div class="no-img-next"><i class="fa fa fa-file-text"></i></div>';
?>
<div class="next"><?php next_post_link( '%link', $next_thumb . '<div class="ntitle">' . $next_post->post_title . '</div><div class="next-arrow"><i class="fa fa-arrow-right pull-right"></i>Next</div>' ); ?></div>
<?php
 }
 else {
?>
<div class="next"><a href="<?php echo THK_HOME_URL; ?>"><i class="fa fa-home navi-home"></i><div class="next-arrow"><i class="fa fa-arrow-right pull-right"></i>Home</div></a></div>
<?php
 }
 $prev_post = get_previous_post();
 if( $prev_post ) {
 $prev_thumb = get_the_post_thumbnail($prev_post->ID, 'thumb100');
 if( empty( $prev_thumb ) ) $prev_thumb = '<div class="no-img-prev"><i class="fa fa fa-file-text fa-rotate-180"></i></div>';
?>
<div class="prev"><?php previous_post_link( '%link', $prev_thumb . '<div class="ptitle">' . $prev_post->post_title . '</div><div class="prev-arrow"><i class="fa fa-arrow-left pull-left"></i>Prev</div>' ); ?></div>
<?php
 }
 else {
?>
<div class="prev"><a href="<?php echo THK_HOME_URL; ?>"><i class="fa fa-home navi-home"></i><div class="prev-arrow"><i class="fa fa-arrow-left pull-right"></i>Home</div></a></div>
<?php
 }
?>
</div><!--/.pnavi-->

「次のページ」(next)のループ部分。

<?php 
 $next_post = get_next_post();
 if( $next_post ) {
 $next_thumb = get_the_post_thumbnail($next_post->ID, 'thumb100');
 if( empty( $next_thumb ) ) $next_thumb = '<div class="no-img-next"><i class="fa fa fa-file-text"></i></div>';
?>

get_next_post();$in_same_termの値を指定します。

$next_post = get_next_post($in_same_term = true);
$in_same_termはカテゴリを判断するパラメータ。
デフォルトはfalse。trueだと同じカテゴリになります。

「次のページ」(next)の表示部分

<div class="next"><?php next_post_link( '%link', $next_thumb . '<div class="ntitle">' . $next_post->post_title . '</div><div class="next-arrow"><i class="fa fa-arrow-right pull-right"></i>Next</div>' ); ?></div>

ここも要領は同じ。

<div class="next"><?php next_post_link( '%link', $next_thumb . '<div class="ntitle">' . $next_post->post_title . '</div><div class="next-arrow"><i class="fa fa-arrow-right pull-right"></i>Next</div>', true ); ?></div>

長くてよくわからないかもですが、next_post_link();のパラメーターはリンク・サムネイル・タイトル・矢印の並び。
一番最後に「true」を入れてあげれば、先程の「$in_same_term = true」と同じになります。
省略しているのは、他にもタクソノミーなどによる分類があるのですが、デフォルトはカテゴリ。
何も書かなければ「true」だけで「$in_same_term=true」と同じだからです。

ここまでがnext(右側)、prev(左側)も同じようにやります。
prevは修正後のみ掲載します。

「前のページ」(prev)のループ部分

<?php
 }
 $prev_post = get_previous_post($in_same_term = true);
 if( $prev_post ) {
 $prev_thumb = get_the_post_thumbnail($prev_post->ID, 'thumb100');
 if( empty( $prev_thumb ) ) $prev_thumb = '<div class="no-img-prev"><i class="fa fa fa-file-text fa-rotate-180"></i></div>';
?>

「前のページ」(prev)の表示部分

<div class="prev"><?php previous_post_link( '%link', $prev_thumb . '<div class="ptitle">' . $prev_post->post_title . '</div><div class="prev-arrow"><i class="fa fa-arrow-left pull-left"></i>Prev</div>', true ); ?></div>

以上で完成です。

そっくり差し替える場合は、こちらをどうぞ。

<?php
if( isset( $luxe['next_prev_nav_visible'] ) ) {
?>
<div id="pnavi" class="grid">
<?php 
 $next_post = get_next_post($in_same_term = true);
 if( $next_post ) {
 $next_thumb = get_the_post_thumbnail($next_post->ID, 'thumb100');
 if( empty( $next_thumb ) ) $next_thumb = '<div class="no-img-next"><i class="fa fa fa-file-text"></i></div>';
?>
<div class="next"><?php next_post_link( '%link', $next_thumb . '<div class="ntitle">' . $next_post->post_title . '</div><div class="next-arrow"><i class="fa fa-arrow-right pull-right"></i>Next</div>', true ); ?></div>
<?php
 }
 else {
?>
<div class="next"><a href="<?php echo THK_HOME_URL; ?>"><i class="fa fa-home navi-home"></i><div class="next-arrow"><i class="fa fa-arrow-right pull-right"></i>Home</div></a></div>
<?php
 }
 $prev_post = get_previous_post($in_same_term = true);
 if( $prev_post ) {
 $prev_thumb = get_the_post_thumbnail($prev_post->ID, 'thumb100');
 if( empty( $prev_thumb ) ) $prev_thumb = '<div class="no-img-prev"><i class="fa fa fa-file-text fa-rotate-180"></i></div>';
?>
<div class="prev"><?php previous_post_link( '%link', $prev_thumb . '<div class="ptitle">' . $prev_post->post_title . '</div><div class="prev-arrow"><i class="fa fa-arrow-left pull-left"></i>Prev</div>', true ); ?></div>
<?php
 }
 else {
?>
<div class="prev"><a href="<?php echo THK_HOME_URL; ?>"><i class="fa fa-home navi-home"></i><div class="prev-arrow"><i class="fa fa-arrow-left pull-right"></i>Home</div></a></div>
<?php
 }
?>
</div><!--/.pnavi-->

Luxeritas Ver3.0以上

Simplicityタイプに変更されました。
考え方はここまでと同じなので細かい説明は割愛します。
前のページの場合はtrueとtrue、次のページならtrueとfalseの組合せです。

ループ部分は前項と同じです。

ページナビの部分を、以下にそっくり差し替えて下さい。

<div id="pnavi" class="grid">
<?php
$wp_upload_dir = wp_upload_dir();

//$next_post = get_next_post();
$next_post = get_adjacent_post( true, '', false , 'category');
if( $next_post ) {
$thumb = 'thumb100';
$image_id = get_post_thumbnail_id( $next_post->ID );
$image_url = wp_get_attachment_image_src( $image_id, $thumb );

if( isset( $image_url[0] ) ) {
$image_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $image_url[0] );

if( file_exists( $image_path ) === false ) {
$thumb = 'thumbnail';
}
}
else {
$thumb = 'thumbnail';
}
$next_thumb = get_the_post_thumbnail( $next_post->ID, $thumb );
if( empty( $next_thumb ) ) $next_thumb = '<div class="no-img-next"><i class="fa fas ' . $fa_file . '"></i></div>';
?>
<div class="next"><?php next_post_link( '%link', $next_thumb . '<div class="ntitle">' . $next_post->post_title . '</div><div class="next-arrow"><i class="fa fas fa-arrow-right ' . $fa_pull_right . '"></i>' . __( 'Next', 'luxeritas' ) . '</div>' , true); ?></div>
<?php
}
else {
?>
<div class="next"><a href="<?php echo THK_HOME_URL; ?>"><i class="fa fas fa-home navi-home"></i><div class="next-arrow"><i class="fa fas fa-arrow-right <?php echo $fa_pull_right; ?>"></i><?php echo __( 'Home ', 'luxeritas' ); ?></div></a></div>
<?php
}
//$prev_post = get_previous_post();
$prev_post = get_adjacent_post( true, '', true , 'category');
if( $prev_post ) {
$thumb = 'thumb100';
$image_id = get_post_thumbnail_id( $prev_post->ID );
$image_url = wp_get_attachment_image_src( $image_id, $thumb );

if( isset( $image_url[0] ) ) {
$image_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $image_url[0] );

if( file_exists( $image_path ) === false ) {
$thumb = 'thumbnail';
}
}
else {
$thumb = 'thumbnail';
}
$prev_thumb = get_the_post_thumbnail( $prev_post->ID, $thumb );
if( empty( $prev_thumb ) ) $prev_thumb = '<div class="no-img-prev"><i class="fa fas ' . $fa_file . ' fa-rotate-180"></i></div>';
?>
<div class="prev"><?php previous_post_link( '%link', $prev_thumb . '<div class="ptitle">' . $prev_post->post_title . '</div><div class="prev-arrow"><i class="fa fas fa-arrow-left ' . $fa_pull_left . '"></i>' . __( 'Prev', 'luxeritas' ) . '</div>' , true); ?></div>
<?php
}
else {
?>
<div class="prev"><a href="<?php echo THK_HOME_URL; ?>"><i class="fa fas fa-home navi-home"></i><div class="prev-arrow"><i class="fa fas fa-arrow-left <?php echo $fa_pull_left; ?>"></i><?php echo __( 'Home ', 'luxeritas' ); ?></div></a></div>
<?php
}
?>
</div><!--/.pnavi-->
スポンサーリンク

結論

解答者の写真
恐らく、どちらかのテーマのパターンには当てはまるんじゃないかな?
要は「true」と「false」を、どこにどのように入れるかってだけ。
これで立派な小説サイトが作れるわね
 
スポンサーリンク

おまけ ~正しく設定したはずなのに動かないケース

その1 カテゴリの付け方に問題がある

質問者の写真

記事の通りにしたはずなのにできない!
 
解答者の写真
記事のカテゴリで親カテゴリにもチェックつけてない?
そうしちゃうと親カテゴリの方でページ送りされるわよ
 

 

例えば小説を一纏めにするカテゴリを「novel」、子カテゴリに小説個別のカテゴリ「novel1」、「novel2」、……とあったとします。
この場合、チェックを付けるのは子カテゴリのnovel1だけにしておかないと、思った通りに動きません。

コードの問題ではなく、見落としやすい設定の罠ということで。

その2 WordPressそのものが壊れてる

質問者の写真

やっぱり動かない!
 

レアケースですが、記述を変更しても動かない場合があります。
原因はプラグインの相性だったり、WordPressそのものがおかしくなってたり。

実は以前、このパターンのトラブルに見舞われました。
結局、WordPressを再インストールしたら解決しました。

解答者の写真
どうしてもダメだと思ったら、思い切ってWordPressの再インストールするのも一つの手よ
 
ブログやサイトを始めたい・引っ越したい方へ

レンタルサーバーWING。
KUSANAGI&WEXALが使えるVPS。
どちらでも高速でコスパに優れたConoHaで始めるのがおすすめです。
もっと知りたい方はボタンをクリックしてください。
(ボタン経由で入会した場合、1000円分のクーポンがもらえます)

この記事を書いた人

広島市内のパチンコホール勤務。
3号機時代からのパチンカス。
ADHD、精神障害者手帳3級所持。
慶應義塾大学商学部卒、専攻はマーケティング(広告・宣伝)
国家一種試験経済職の資格で公安調査庁に入庁。
在職時は国際テロ、北朝鮮を担当。
「小説家になろう」の底辺作者。
WordPress記事は素人の備忘録です。

天満川鈴をフォローする
カスタマイズ&プラグイン
スポンサーリンク

コメント

タイトルとURLをコピーしました