WORDPRESSの「前の記事へ」「次の記事へ」をカスタマイズ。同じカテゴリの記事のみタイトルを文字数制限して表示。
こんにちは、福田です!
最近ちょっとずつ暖かくなってきて嬉しいです。
そろそろ桜も咲き始めそうですね
今日は、WORDPRESSの投稿などで使用する「前の記事へ」、「次の記事へ」ボタンを少しカスタマイズしてみようって話です。
【カスタマイズの内容】
・「前の記事へ」や「次の記事へ」ではなく、記事のタイトルを文字数制限して表示する。
・同じカテゴリの記事のみで遷移できるようにする。
↑ここがポイントです。
記事のカテゴリが「ブログ」「お知らせ」「お客様の声」「よくある質問」みたいにいくつもある場合、
同じカテゴリの記事だけ読ませたい!って時に使えます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<ul> <li class="nextPost"> <?php $previous_post = get_previous_post(true, ''); $pre_post_title = $previous_post->post_title; if (mb_strlen($pre_post_title) > 18) { $pre_post_title = mb_substr($pre_post_title, 0, 18) . '...'; } if (!empty($previous_post)): ?> <a href="<?php echo esc_url(get_permalink($previous_post->ID)); ?>" title="<?php echo $previous_post->post_title; ?>"><?php echo $pre_post_title; ?></a> <?php endif; ?> </li> <li class="prevPost"> <?php $next_post = get_next_post(true, ''); $next_post_title = $next_post->post_title; if (mb_strlen($next_post_title) > 18) { $next_post_title = mb_substr($next_post_title, 0, 18) . '...'; } if (!empty($next_post)): ?> <a href="<?php echo esc_url(get_permalink($next_post->ID)); ?>" title="<?php echo $next_post->post_title; ?>"><?php echo $next_post_title; ?></a> <?php endif; ?> </li> </ul> |
この場合、文字数制限は18文字にしてます。
18文字を超えるタイトルの場合は、19文字目から”…”で置き換えられます。
また、
get_previous_post(true, ”);
get_next_post(true, ”);
この部分で同じカテゴリの記事のみ表示させてます。
全ての記事を表示させたい場合は、オプションを何も入力せずに
get_previous_post();
get_next_post();
でOKです。
それでは~。
花粉症対策にはヨーグルトを食べましょう