【wordpress】記事の表示
トップページに最新の記事を表示させる。
1ページに4件の記事を表示して、古い記事はページを変えて閲覧できるようにする。
home.php、content.php、pagenation.php。
◼︎home.php
記事のタイトルを表示する
<?php if(have_posts()): while(have_posts()): the_post(); ?> <?php get_template_part('content'); ?> <?php endwhile; endif; ?> <?php get_template_part('pagenation'); ?>
ループの中には記事の出力内容を指定する設定を記述する。
ただし、この設定はcontent.phpに記述して複数のテンプレートを共有できるようにする。
ページのタイトルを追加する(RECENT POST)
RECENT POSTS
古い記事も閲覧できるようにする
<?php get_template_part('pagenation'); ?>
◼︎content.php
記事のタイトルを表示する
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p class="postcat"><?php the_category(' '); ?></p>
記事のカテゴリー、本文、投稿年月日を表示する。
<div class="post"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p class="postcat"><?php the_category(' '); ?></p> <?php the_content(); ?> <p class="postinfo"> <?php echo get_the_date(); ?> </p> </div>
◼︎pagenation.php
このファイルはカテゴリーページと月別ページで使う。
<p class="pagenation"> <span class="oldpage"><?php next_posts_link('« 古い記事'); ?></span> <span class="newpage"><?php previous_posts_link('新しい記事 »'); ?></span> </p>