テーマ解析:ブランクテーマ『underscores』
ワードプレスのテーマを制作したくても、何からしたらいいか迷いませんか?
子テーマでカスタマイズが一番楽な方法だと思いますが
テーマを制作を理解するためにブランクテーマを利用したテーマ制作をしてみます。
ブランクテーマはワードプレスのテーマで最小限のファイル・機能のみで作成しているのでカスタマイズして新たなテーマを制作するのに適しています。
そこで人気のブランクテーマ『underscores』を解析して最低限の機能・構造を調べました。
まずはテーマ名(例:test)を記入してGENEATEをクリック
Advanced Optionsをクリックで他の項目を設定可能
すぐにダウンロードが完了するので解凍してください。
まずは必要のないファイルを削除します。
README.md:説明文
公式テーマ作成の場合に必要なファイル
license.txt:ライセンスの記載
重要な部分から
index.php
<?php
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) :
if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php
endif;
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
動作の流れ
index.php
・ヘッダーを読み込む。header.php テンプレートファイルを読み込みます。
get_header();
関数リファレンス/get header - WordPress Codex 日本語版
・コンテンツ開始部分のHTMLコード
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
・投稿データが存在するかの確認
if ( have_posts() ) :
関数リファレンス/have posts - WordPress Codex 日本語版
分岐(1):投稿データがある場合
分岐(2):ブログ投稿インデックスページでフロントページでなければ
・個別記事ページでループ外に記事のタイトルを出力します。
テンプレートタグ/single post title - WordPress Codex 日本語版
分岐(2):その他の条件
ループで投稿データを出力
while ( have_posts() ) : the_post();
the_postループで次の投稿データを表示する時にテンプレートを読み込む
template-parts/content.php
get_template_part( 'template-parts/content', get_post_format() );
関数リファレンス/get post format - WordPress Codex 日本語版
投稿記事の投稿フォーマットを返します。
ループ終了時
the_posts_navigation() | Function | WordPress Developer Resources
次の投稿ページ、または前の投稿ページへのナビゲーション用
分岐(1):投稿データがない場合
関数リファレンス/get template part - WordPress Codex 日本語版
template-parts/content-none.phpを読み込む
・コンテンツ終了部分のHTMLコード
</main>
</div>
・サイドバーの読み込み。sidebar.phpを読み込む
get_sidebar();
関数リファレンス/get sidebar - WordPress Codex 日本語版
・フッターの読み込み。footer.phpを読み込む
get_footer();
次のテンプレート調査は次回更新します。