archive.php
archive.phpを見ていきます。
<?php /** * The template for displaying archive pages * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/ * * @package yottagin_s */ get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main"> <?php if ( have_posts() ) : ?> <header class="page-header"> <?php the_archive_title( '<h1 class="page-title">', '</h1>' ); the_archive_description( '<div class="archive-description">', '</div>' ); ?> </header><!-- .page-header --> <?php /* Start the Loop */ while ( have_posts() ) : the_post(); /* * Include the Post-Type-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 Type name) and that will be used instead. */ get_template_part( 'template-parts/content', get_post_type() ); endwhile; the_posts_navigation(); else : get_template_part( 'template-parts/content', 'none' ); endif; ?> </main><!-- #main --> </div><!-- #primary --> <?php get_sidebar(); get_footer();
single.phpと似ていますが、if-elseが入っている分少し読みにくいので、php部分だけ抜き出して、馴染みやすい形にします。
<?php
//表示するアーカイブがある時は…
if ( have_posts() ) {
//そのアーカイブのタイトルを表示する。
the_archive_title( '<h1 class="page-title">', '</h1>' );
//そのアーカイブのディスクリプションを表示する。
the_archive_description( '<div class="archive-description">', '</div>' );
//表示するアーカイブがある限りWordPressループを回す。
while ( have_posts() ) {
//記事を選択して、ポストタイプに従ったテンプレートで表示する。
//普通はcontent.phpを読み込まれる。
the_post();
get_template_part( 'template-parts/content', get_post_type() );
}
//前後のナビゲーションを表示する。
the_posts_navigation();
}
//表示するアーカイブがない時は…
else {
//content-none.php を読み込む。
get_template_part( 'template-parts/content', 'none' );
}
アーカイブがある時はそのタイトルと内容を表示する、アーカイブがない時は何もありませんと表示します。
また、 get_template_part( ‘template-parts/content’, get_post_type() ); のコメントで、 get_post_type()をわざわざ第2引数に使う意図が書かれています。single.phpの時にわざわざこう書く理由が良くわからなかったのですが、子テーマを作成するときに、異なるテンプレートを読み込むような拡張がしやすいようこう書いているようです。
search.php
search.phpを見てみます。
<?php /** * The template for displaying search results pages * * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result * * @package _s */ get_header(); ?> <section id="primary" class="content-area"> <main id="main" class="site-main"> <?php if ( have_posts() ) : ?> <header class="page-header"> <h1 class="page-title"> <?php /* translators: %s: search query. */ printf( esc_html__( 'Search Results for: %s', '_s' ), '<span>' . get_search_query() . '</span>' ); ?> </h1> </header><!-- .page-header --> <?php /* Start the Loop */ while ( have_posts() ) : the_post(); /** * Run the loop for the search to output the results. * If you want to overload this in a child theme then include a file * called content-search.php and that will be used instead. */ get_template_part( 'template-parts/content', 'search' ); endwhile; the_posts_navigation(); else : get_template_part( 'template-parts/content', 'none' ); endif; ?> </main><!-- #main --> </section><!-- #primary --> <?php get_sidebar(); get_footer();
archive.phpとほぼ変わりません。同じようにphp部分だけ抜き出してみます。
<?php
//表示するアーカイブがある時は…
if ( have_posts() ) {
// Search Results for 検索単語 と表示する。
printf( esc_html__( 'Search Results for: %s', '_s' ), '<span>' . get_search_query() . '</span>' );
//表示する検索結果がある限りWordPressループを回す。
while ( have_posts() ) {
the_post();
//contnt-search.phpというテンプレートファイルを読み込む。
get_template_part( 'template-parts/content', 'search' );
}
//前後のナビゲーションを表示する。
the_posts_navigation();
//表示するアーカイブがない時は…
}
//表示するアーカイブがない時は…
else {
//content-none.php を読み込む。
get_template_part( 'template-parts/content', 'none' );
}
検索結果があればそれを表示します、なければ何もありませんと表示します。