トップページを作成する
最新記事の一覧を表示したトップページを作成し、各記事のページにアクセスできるようにします。
- ヘッダーとフッターをテンプレートパーツ化
- アイキャッチ画像の設定
- 最新記事の画像とタイトルをリストアップ
- 記事一覧を2列でレイアウト
- ページネーションを表示
目次
ヘッダーとフッターをテンプレートパーツ化
トップページを作成していく前に、トップページと記事ページに共通するパーツ(ヘツダーとフッター)をテンプレートパーツ化して、設定を共有できるようにします。
<?php get_header(); ?>
<?php get_footer(); ?>
テンプレートパーツ
ヘッダーやフッターなどのパーツは テンプレートパーツとして設定 管理 、index.php などのテンプレートに読 込んで利用することができます 利用できるテンプレート パーツは以下のようになっております。
| パーツ | テンプレートパーツのファイル名 | 読み込むためのテンプレートタグ |
| ヘッダー | header.php | <?php get_header(); ?> |
| フッター | footer.php | <?php get_footer(); ?> |
| サイドバーなどのメニュー | sidebar.php | <?php get_sidebar(); ?> |
| コメント | comments.php | <?php comments_template(); ?> |
| 検索フォーム | searchform.php | <?php get_search_form(); ?> |
| カスタム | *.php | <?php get_template_part(‘*’); ?> |
カスタ厶テンプレートパーツ
カスタムテンプレートパーツは任意のファイル名で作成し、<?php get_template_part(); ?> を使って読み込みます。例えば、parts.php というファイル名でテンプレートパーツを用意した場合、<?php get_template_part('parts'); ?> として読み込むことができます。
ハイフン区切りで parts-list.php というファイル名で用意した場合、 読み込みます。例えば、parts.php というファイル名でテンプレートパーツを用意した場合、<?php get_template_part('parts-list'); ?> と指定することもできますが <?php get_template_part('parts', 'list'); ?> として指定することも可能です
トップページに記事のタイトルをリストアップする
トップページ用のテンプレートを作成し、表示をアレンジしていきます。
トップページ用のテンプレートとなる home.php を作成します。
index.php をコピーして home.php を作成し トップページに必要なコードを追記したり不要なコートは削除してトップページを作成していきます。
6件の記事をタイトルをリストアップするように指定します。
表示件数の指定方法は、ダッシュボート->設定->表示設定
1ページに表示する最大投稿数 にて指定します。
テンプレート階層
WordPress では生成ページごとに使用されるテンプレートが決まっており、テーマフォルダ内に存在する優先順位の高いテンプレートが使用されます。
index.php はすべてのページの生成に使用されるテンプレートな為、優先順位は最も低く設定されています。特定のページ用のテンプレートが用意されていた場合、そちらを使って生成されることになります。
たとえば、トップページは home.php や index.php で生成されますが、テーマフォルダ内に両方のファイルが存在した場合、優先順位の高い home.php が使用されます。
| ページの種類 | 高い | 優先順位 | 低い |
| 記事ページ | single.php | singular.php | index.php |
| 固定ページ | page.php | singular.php | index.php |
| トップページ | front-page.php | home.php | index.php |
| カテゴリページ | category.php | archive.php | index.php |
| 月別ページ | date.php | archive.php | index.php |
さらに細かく、個別の記事用のテンプレートを用意するといったことも可能です。使用できるテンプレートのファイル名や優先順位について詳しくは、下記のページを参考にしてください。

Template Hierarchy
記事のサムネイル画像を表示るす
以下のコートを functions.php に追記することで、投稿時にアイキャッチ画像を選択できるようになる。
add_theme_support('post-thumbnails');
アイキャッチ画像を表示する場合は以下のコードを呼び出す。
<?php the_post_thumbnail(); ?>
<?php if( has_post_thumbnail() ): ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
作成ファイル一覧
editor-style.css
@charset "UTF-8";
/* 変数 */
:root {
--f1: 2.44em; /* 39.04px */
--f2: 1.95em; /* 31.2px */
--f3: 1.56em; /* 24.96px */
--f4: 1.25em; /* 20px */
--f5: 1em; /* 16px */
--f6: 0.8em; /* 12.8px */
--basecolor: #63a211; /* 基本色 */
}
/* フォント */
body,
.editor-post-title__input {
font-family: sans-serif;
}
p {
line-height: 1.8;
font-size: var(--f5);
}
h1 {
font-size: var(--f1);
}
h2 {
font-size: var(--f2);
}
h3 {
font-size: var(--f3);
}
h4 {
font-size: var(--f4);
}
h5 {
font-size: var(--f5);
}
h6 {
font-size: var(--f6);
}
h2 {
border-left: solid 14px var(--basecolor);
padding-left: 14px;
}
[data-align="center"] .wp-block-categories,
[data-align="center"] .wp-block-latest-posts,
[data-align="center"] .wp-block-archives,
[data-align="center"] .wp-block-tag-cloud,
[data-align="center"] .wp-block-latest-comments,
[data-align="center"] .wp-block-rss {
text-align: center;
}
/* 横幅 */
.wp-block {
max-width: 720px;
}
<footer class="myfoot">
<?php bloginfo( 'name' ); ?>
</footer>
<?php wp_footer(); ?>
</body>
</html>
functions.php
<?php
function mytheme_setup() {
// (C)のCSSを有効化
add_theme_support( 'wp-block-styles' );
// 縦横比を維持したレスポンシブを有効化
add_theme_support( 'responsive-embeds' );
// (E)のCSSを有効化&エディタに読み込み
add_theme_support( 'editor-styles' );
add_editor_style( 'editor-style.css' );
// ページのタイトルを有効化
add_theme_support( 'title-tag' );
// link、style、scriptのHTML5対応を有効化
add_theme_support( 'html5', array(
'style',
'script'
) );
// アイキャッチ画像を有効化
add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );
function mytheme_enqueue() {
// Google Fontsを読み込み
wp_enqueue_style(
'myfonts',
'https://fonts.googleapis.com/css?family=Josefin+Sans:300,700&display=swap',
array(),
null
);
// Dashiconsを読み込み
wp_enqueue_style(
'dashicons'
);
// (D)テーマのCSSを読み込み
wp_enqueue_style(
'mytheme-style',
get_stylesheet_uri(),
array(),
filemtime( get_theme_file_path( 'style.css' ) )
);
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue' );
header.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<header class="myhead mycontainer">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php bloginfo( 'name' ); ?>
</a>
<p><?php bloginfo( 'description' ); ?></p>
</header>
home.php
<?php get_header(); ?>
<main class="mycontainer">
<div class="myposthead">
<h1>最新情報</h1>
<p>RECENT</p>
</div>
<div class="mypostlist">
<?php if(have_posts()): while(have_posts()):
the_post(); ?>
<article <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>">
<?php if( has_post_thumbnail() ): ?>
<figure>
<?php the_post_thumbnail(); ?>
</figure>
<?php endif; ?>
<h2><?php the_title(); ?></h2>
</a>
</article>
<?php endwhile; endif; ?>
</div>
<?php the_posts_pagination( array(
'prev_text' => '<span class="dashicons dashicons-arrow-left-alt2"></span><span class="screen-reader-text">前へ</span>',
'next_text' => '<span class="screen-reader-text">次へ</span><span class="dashicons dashicons-arrow-right-alt2"></span>'
) ); ?>
</main>
<?php get_footer(); ?>
index.php
<?php get_header(); ?>
<?php if(have_posts()): while(have_posts()):
the_post(); ?>
<article <?php post_class( 'mycontainer' ); ?>>
<div class="myposthead">
<?php the_category(); ?>
<h1><?php the_title(); ?></h1>
<time datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>">
<?php echo esc_html( get_the_date() ); ?>
</time>
</div>
<?php the_content(); ?>
<?php the_post_navigation(); ?>
</article>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
style.css
@charset "UTF-8";
/*
Theme Name: MY THEME 5
Author: MONIKER
Description: オリジナルテーマです
Version: 1.0
*/
img {
max-width: 100%;
height: auto;
}
.alignleft {
float: left;
margin-left: 0;
margin-right: 1em;
}
.alignright {
float: right;
margin-left: 1em;
margin-right: 0;
}
.aligncenter {
margin-left: auto;
margin-right: auto;
}
.wp-block-categories.aligncenter,
.wp-block-latest-posts.aligncenter,
.wp-block-archives.aligncenter,
.wp-block-tag-cloud.aligncenter,
.wp-block-latest-comments.aligncenter,
.wp-block-rss.aligncenter {
text-align: center;
}
figure {
margin-left: 0;
margin-right: 0;
}
body {
box-sizing: border-box;
}
body *,
body *::before,
body *::after {
box-sizing: inherit;
}
/* 変数 */
:root {
--f1: 2.44em; /* 39.04px */
--f2: 1.95em; /* 31.2px */
--f3: 1.56em; /* 24.96px */
--f4: 1.25em; /* 20px */
--f5: 1em; /* 16px */
--f6: 0.8em; /* 12.8px */
--basecolor: #63a211; /* 基本色 */
--gradient: linear-gradient( 79.7deg, rgba(34,126,34,1) 8.2%, rgba(99,162,17,1) 84.9% );
--myfont: 'Josefin Sans', sans-serif;
}
/* フォント */
body {
font-family: sans-serif;
}
p {
line-height: 1.8;
font-size: var(--f5);
}
h1 {
font-size: var(--f1);
}
h2 {
font-size: var(--f2);
}
h3 {
font-size: var(--f3);
}
h4 {
font-size: var(--f4);
}
h5 {
font-size: var(--f5);
}
h6 {
font-size: var(--f6);
}
h2 {
border-left: solid 14px var(--basecolor);
padding-left: 14px;
}
/* 横幅 */
body {
margin: 0;
}
.mycontainer {
margin-left: 10px;
margin-right: 10px;
}
@media (min-width: 740px) {
.mycontainer {
max-width: 720px;
margin-left: auto;
margin-right: auto;
}
}
/* 記事に関する情報 */
.myposthead {
padding-top: 20px;
padding-bottom: 10px;
text-align: center;
}
.myposthead p {
color: var(--basecolor);
font-family: var(--myfont);
font-weight: bold;
}
/* リンク */
a {
color: inherit;
text-decoration: none;
}
a:hover {
opacity: 0.7;
}
/* 記事が属するカテゴリー */
.post-categories {
display: flex;
justify-content: center;
padding: 0;
list-style-type: none;
}
.post-categories li:not(:last-child) {
margin-right: 10px;
}
.post-categories a {
padding: 3px 10px;
border: solid 1px currentColor;
border-radius: 10px;
color: var(--basecolor);
font-size: var(--f6);
}
.post-categories::before {
content: "\f318";
font-family: "dashicons";
color: var(--basecolor);
font-size: var(--f2);
line-height: 1;
margin-right: 5px;
}
/* スクリーンリーダー用のテキスト */
.screen-reader-text {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
word-wrap: normal !important;
}
/* 前後の記事へのリンク */
.nav-links {
display: flex;
margin: 30px 0;
font-size: var(--f6);
}
.nav-previous {
flex: 1;
display: flex;
align-items: center;
}
.nav-next {
flex: 1;
margin-left: 10px;
text-align: right;
display: flex;
align-items: center;
justify-content: flex-end;
}
.nav-previous::before {
content: "\f141";
}
.nav-next::after {
content: "\f139";
}
.nav-previous::before, .nav-next::after {
font-family: "dashicons";
color: var(--basecolor);
font-size: var(--f2);
line-height: 1;
}
/* ヘッダー */
.myhead {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 0;
color: #444444;
}
.myhead a {
font-size: var(--f3);
font-family: var(--myfont);
}
.myhead p {
margin: 0;
font-size: var(--f6);
}
/* フッター */
.myfoot {
padding: 16px;
background-color: var(--basecolor);
background-image: var(--gradient);
color: #ffffff;
text-align: center;
font-family: var(--myfont);
}
/* 記事一覧 */
.mypostlist {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.mypostlist article {
width: 48%;
margin-bottom: 20px;
}
.mypostlist figure {
margin: 0;
}
.mypostlist img {
height: 180px;
object-fit: cover;
}
.mypostlist h2 {
margin: 0;
padding: 0;
border: none;
font-size: var(--f6);
}
/* ページネーション */
.pagination .nav-links {
margin-bottom: 50px;
justify-content: center;
}
.pagination .page-numbers:not(:last-child) {
margin-right: 15px;
}
.pagination a {
color: var(--basecolor);
}
.pagination .current {
color: #aaaaaa;
}
screenshot.jpg
