概要と目標
サイト全体の共通パーツや、
記事の個別ページを作ろう。
ヘッダー、フッター、サイドバーなどのサイト全体の共通パーツや、
記事の一覧ページや個別ページなどのテンプレートファイルを作れるようになりましょう。
ヘッダー、フッター、サイドバーなどのサイト全体の共通パーツや、
記事の一覧ページや個別ページなどのテンプレートファイルを作れるようになりましょう。
WordPressでは、表示するページの役割によって、使用されるテンプレートのファイル名は決まっている。
そして、そのファイル名のファイルが存在しない時に、代わりに使用するファイルも決まっている。
どのテンプレートのファイルも存在しない時は、「index.php」を使って表示される。
| ページの種類 |
テンプレート階層の優先順位
|
||||
|---|---|---|---|---|---|
| ホームページ | 最新の投稿 | front-page.php | home.php | index.php | |
| 固定ページ | front-page.php | ||||
| 投稿インデックスページ | home.php | ||||
| アーカイブページ | 作者ページ | author.php | archive.php | ||
| カテゴリーページ | category.php | ||||
| タグページ | tag.php | ||||
| 月別アーカイブ | date.php | ||||
| カスタム投稿タイプページ | archive-[カスタム投稿タイプ名].php | ||||
| カスタムタクソノミーページ | taxonomy.php | ||||
| 個別ページ | 投稿記事ページ | single-post.php | single.php | singular.php | |
| カスタム投稿記事ページ | single-[カスタム投稿タイプ名].php | ||||
| 添付ファイルページ | attachment.php | ||||
| 固定ページ | page.php | ||||
| 検索結果ページ | search.php | ||||
| 404ページ | 404.php | ||||
テンプレートファイル以外にも、サイト全体で共通化するパーツも、
決まったファイル名で保存しておくと、簡単に読み込むことが出来る。
| 共通パーツ | テンプレートのファイル名 | 読み込む関数 |
|---|---|---|
| ヘッダー | header.php | get_header(); |
| フッター | footer.php | get_footer(); |
| サイドバー | sidebar.php | get_sidebar(); |
| コメント | comments.php | get_comments_template(); |
| 検索フォーム | searchform.php | get_search_form(); |
サイト全体で使うヘッダーは、header.phpに記述して、
get_header(); 関数で読み込むことで共通化できる。
get_header 関数 ・・・ header.phpを読み込む関数get_header(ヘッダー名)
省略可
引数の詳細はWordPress Codex 日本語版を参照
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<header class="l-header p-header">
<div class="l-container">
<div class="p-header__info">
<h1 class="p-header__title"><a href="<?php echo esc_url( home_url() ); ?>" class="p-logo"><?php bloginfo('name'); ?></a></h1>
<p class="p-header__text"><?php bloginfo('description'); ?></p>
</div>
<!-- /.p-header__info -->
<nav class="p-header__nav">
<h2 class="screen-reader-text">サイト内メニュー</h2>
<button class="js-drawer c-button p-hamburger" aria-controls="globalNav" aria-expanded="false">
<span class="p-hamburger__line">
<span class="screen-reader-text">メニューを開閉</span>
</span>
</button>
<?php
$args = [
'theme_location' => 'global',
'menu_class' => 'p-globalNav',
'menu_id' => 'globalNav',
'container' => false
];
wp_nav_menu($args);
?>
</nav>
</div>
<!-- /.p-header__inner -->
</header>
get_header()に置き換える<?php get_header(); ?>
<div class="l-contents">
<div class="l-container">
<main class="l-main">
サイト全体で使うフッターは、footer.phpに記述して、
get_footer(); 関数で読み込むことで共通化できる。
get_footer 関数 ・・・ footer.phpを読み込む関数get_footer(フッター名)
省略可
引数の詳細はWordPress Codex 日本語版を参照
<footer class="l-footer">
<div class="l-container">
<?php if ( is_active_sidebar( 'footer' ) ) : ?>
<div class="c-grid -gutter">
<?php dynamic_sidebar( 'footer' ); ?>
</div>
<!-- /.c-grid -->
<?php endif; ?>
<p class="l-footer__text"><small>© 2019 Dummy.</small></p>
</div>
<!-- /.l-container -->
</footer>
<?php wp_footer(); ?>
</body>
</html>
get_footer()に置き換える </main>
<?php if ( is_active_sidebar( 'sidebar' ) ) : ?>
<div class="l-sub">
<?php dynamic_sidebar( 'sidebar' ); ?>
</div>
<!-- /.l-sub -->
<?php endif; ?>
</div>
<!-- /.l-container -->
</div>
<!-- /.l-contents -->
<?php get_footer(); ?>
サイト全体で使うサイドバーは、sidebar.phpに記述して、
get_sidebar(); 関数で読み込むことで共通化できる。
get_sidebar 関数 ・・・ sidebar.phpを読み込む関数get_sidebar(サイドバー名)
省略可
引数の詳細はWordPress Codex 日本語版を参照
<?php if ( is_active_sidebar( 'sidebar' ) ) : ?>
<div class="l-sub">
<?php dynamic_sidebar( 'sidebar' ); ?>
</div>
<!-- /.l-sub -->
<?php endif; ?>
get_sidebar()に置き換える </main>
<?php get_sidebar(); ?>
</div>
<!-- /.l-container -->
</div>
<!-- /.l-contents -->
<?php get_footer(); ?>
サイト全体で使うコンポーネントは、外部ファイル化しておくと
get_template_part(); 関数で読み込むことで共通化できる。
get_template_part 関数 ・・・ テンプレートパーツ(ヘッダー、サイドバー、フッターを除きます)を読み込む関数get_template_part(テンプレート名, 特定のテンプレート名)
省略可
引数の詳細はWordPress Codex 日本語版を参照
<article class="c-media p-article">
<div class="c-media__body">
<h3 class="p-article__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_category(); ?>
<p class="p-article__text"><time datetime="<?php the_time( DATE_W3C ); ?>" class="p-article__time"><?php the_time( get_option( 'date_format' ) ); ?></time></p>
</div>
<figure class="c-media__thumbnail">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail( 'dummy-thumbnail' ) ?>
<?php else : ?>
<img src="<?php echo esc_url(get_theme_file_uri()); ?>/assets/images/no-picture.png" srcset="<?php echo esc_url(get_theme_file_uri()); ?>/assets/images/no-picture.png, <?php echo esc_url(get_theme_file_uri()); ?>/assets/images/no-picture@2x.png 2x" alt="アイキャッチ画像はありません">
<?php endif; ?>
</figure>
<!-- /.c-media__thumbnail -->
</article>
get_template_part()に置き換える<?php if ( have_posts() ) :?>
<?php while( have_posts() ) : the_post(); ?>
<?php get_template_part('template-parts/content'); ?>
<?php endwhile; ?>
<?php
// ページネーション
$args = [
'prev_text' => '<span class="screen-reader-text">前へ</span>',
'next_text' => '<span class="screen-reader-text">次へ</span>'
];
the_posts_pagination( $args );
?>
<?php else : ?>
個別の投稿を専用のページで表示させる場合は、
「single.php」というファイルを作ることで対応できる。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>記事のタイトル | Dummy</title>
<link rel="stylesheet" href="assets/css/common.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Sans+JP:400,700|Quicksand:400,700&display=swap&subset=japanese">
</head>
<body>
<header class="l-header p-header">
<div class="l-container">
<div class="p-header__info">
<h1 class="p-header__title"><a href="/" class="p-logo">Dummy</a></h1>
<p class="p-header__text">Just Another</p>
</div>
<!-- /.p-header__info -->
<nav class="p-header__nav">
<h2 class="screen-reader-text">サイト内メニュー</h2>
<button class="js-drawer c-button p-hamburger" aria-controls="globalNav" aria-expanded="false">
<span class="p-hamburger__line">
<span class="screen-reader-text">メニューを開閉</span>
</span>
</button>
<ul id="globalNav" class="p-globalNav">
<li><a href="index.html">Home</a></li>
<li><a href="page.html">About Me</a></li>
<li><a href="page.html">Works</a></li>
<li><a href="page.html">Contact</a></li>
</ul>
</nav>
</div>
<!-- /.p-header__inner -->
</header>
<?php get_header(); ?>
<footer class="l-footer">
<div class="l-container">
<div class="c-grid -gutter">
<div class="c-grid__item -tab4Of12">
<aside id="calendar-3" class="p-widget -dark widget_calendar">
<h2 class="p-widget__title">プロフィール</h2>
<div class="textwidget custom-html-widget">
ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。ここにプロフィールの文章。
</div>
</aside>
</div>
<div class="c-grid__item -tab4Of12">
<aside id="categories-5" class="p-widget -dark widget_categories">
<h2 class="p-widget__title">最近の投稿</h2>
<ul>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
</ul>
</aside>
</div>
<div class="c-grid__item -tab4Of12">
<aside id="archives-3" class="p-widget -dark widget_archive">
<h2 class="p-widget__title">カテゴリー</h2>
<ul>
<li><a href="#">カテゴリ名</a></li>
<li><a href="#">カテゴリ名</a></li>
<li><a href="#">カテゴリ名</a></li>
</ul>
</aside>
</div>
</div>
<!-- /.c-grid -->
<p class="l-footer__text"><small>© 2019 Dummy.</small></p>
</div>
<!-- /.l-container -->
</footer>
<script src="http://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src="assets/js/app.js"></script>
</body>
</html>
<?php get_footer(); ?>
<div class="l-sub">
<aside class="p-widget -light">
<h2 class="p-widget__title">サイト内検索</h2>
<div class="p-widgets__body">
<form role="search" method="get" class="search-form" action="#">
<label>
<span class="screen-reader-text">検索:</span>
<input type="search" class="search-field" placeholder="検索 …" value="" name="s" />
</label>
<input type="submit" class="search-submit" value="検索" />
</form>
</div>
<!-- /.widgets__body -->
</aside>
<aside class="p-widget -light">
<h2 class="p-widget__title">最近の投稿</h2>
<ul>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
<li>
<a href="single.html">記事タイトル記事タイトル記事タイトル記事タイトル</a>
<span class="post-date">2019年6月19日</span>
</li>
</ul>
</aside>
<aside class="p-widget -light">
<h2 class="p-widget__title">カテゴリー</h2>
<ul>
<li><a href="#">カテゴリ名</a></li>
<li><a href="#">カテゴリ名</a></li>
<li><a href="#">カテゴリ名</a></li>
</ul>
</aside>
</div>
<!-- /.l-sub -->
<?php get_sidebar(); ?>
WordPressでは個別ページでもループを使って投稿内容を取得する。
<?php get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="l-contents">
<div class="l-container">
<main class="l-main">
</div>
<!-- /.l-contents -->
<?php endwhile; ?>
<?php get_footer(); ?>
<?php the_title(); ?>に置き換える<h1 class="p-article__title -detail"><?php the_title(); ?></h1>
<?php if ( has_post_thumbnail()) : ?>
<figure class="p-article__eyecatch">
<?php the_post_thumbnail(); ?>
</figure>
<?php endif; ?>
<p class="p-article__text">
<time datetime="<?php the_time( DATE_W3C ); ?>" class="p-article__time">
<?php the_time( get_option( 'date_format' ) ); ?>
</time>
</p>
<?php the_category(); ?>
<?php the_content(); ?>に置き換える<section class="p-article__body">
<?php the_content(); ?>
</section>
<?php the_tags(); ?>に置き換える<footer class="p-article__footer">
<p class="p-article__text"><?php the_tags(); ?></p>
著者情報もテンプレートタグを使うことで表示することができる。
著者の情報を表示するテンプレートタグは様々なものがあるが、
今回は、著者の名前を、著者の投稿一覧ページへのリンク付きで表示するthe_author_posts_link() を使う。
the_author_posts_link 関数 ・・・ 投稿者の投稿ページへのリンクを表示する関数the_author_posts_link()
引数の詳細はWordPress Codex 日本語版を参照
the_author_posts_link()関数で置き換える。
<footer class="p-article__footer">
<p class="p-article__text"><?php the_tags(); ?></p>
<section class="p-author">
<h2 class="p-author__title">この記事を書いた人</h2>
<p class="p-author__name"><?php the_author_posts_link(); ?></p>
</section>
</footer>
| 表示する情報 | 関数名 |
|---|---|
| 著者名を表示 | the_author() |
| 著者のメタ情報を表示 | the_author_meta() |
個別ページに、前のページへのリンクと、次のページへのリンクを表示する。
前後の投稿へのリンクは、
the_post_navigation()関数を使って表示できる。
the_post_navigation 関数 ・・・ 前後の投稿ナビゲーションメニューを表示する関数the_post_navigation(オプションの連想配列)
省略可
引数の詳細はWordPress Codex 日本語版を参照
| キー | 値 | 初期値 |
|---|---|---|
'prev_text' |
前ページリンクの表記 |
'%title' |
'next_text' |
次ページリンクの表記 | '%title' |
'in_same_term' |
現在の投稿と同じタクソノミーのみを対象にするか | false |
'taxonomy' |
タクソノミーの名前を指定 ('in_same_term' が true の時のみ有効) |
'category' |
'screen_reader_text' |
ナビゲーションの見出し | __( 'Post navigation' ) |
the_post_navigation()関数で置き換える
<footer class="p-article__footer">
<p class="p-article__text"><?php the_tags(); ?></p>
<section class="p-author">
<h2 class="p-author__title">この記事を書いた人</h2>
<p class="p-author__name"><?php the_author_posts_link(); ?></p>
</section>
</footer>
<?php
$args = [
'prev_text' => '前の記事<br>%title',
'next_text' => '次の記事<br>%title',
];
the_post_navigation($args);
?>
ヘッダーやフッターなどの共通パーツはテンプレート化できる。
また、個別投稿ページは「single.php」というファイルで作る。
get_header()で読み込むget_footer()で読み込むget_sidebar()で読み込む