以下の事をしようとしています。(未来のド忘れした自分に向けて書いてます)
- 固定ページにカスタム投稿タイプ【hana】に投稿したの分類別一覧を年度別に自動抽出
- 「カスタム投稿タイプ【hana】」への投稿はカスタム分類(タクソノミー)で分ける
- 分類は赤(red)、青(blue)の2種類とします。
- 上記をすべて1つのテンプレートにまとめる
あとで管理する時にテンプレートが分かれていると、物忘れの激しい私は混乱するのでちょっと長くなるけどまとめてしまおうという魂胆です。
カテゴリーやタグを使わずにカスタム分類を使用する理由は、
カテゴリーは別の目的で使用中だった事と、タグは管理が面倒なのでチェックボックスが使えるカスタム分類を採用します。
下準備
- カスタム投稿タイプ「お花」を作る。(Post Type Slugは「flower」)
- 1で作った投稿タイプに下記2つのタクソノミーをつくります。※()内はTaxonomy Slug
赤い花(red)
青い花(blue)
この時、Edit Taxonomiesの画面で赤い花と青い花の階層を「True」にしておけば、記事投稿時にチェックボックスを使えるようになります。 - カスタム分類を追加すると、↓こんな感じになります。
- カスタム分類の「赤い花」と「青い花」にそれぞれスラッグをいくつか追加。
- 新規追加画面はこんな感じになります。
テンプレートを作る
↓こんな構成になるテンプレートを作ります。
※オレンジ文字は固定ページのID番号。ピンク文字はスラッグ名。
お花の記録(固定ページ100) | ||
├ | アネモネの記録 記事一覧(固定ページ200)red01 | |
| | ├ | (カスタム投稿タイプ「お花」>赤い花>アネモネの記録) |
| | アンスリウムの記録 記事一覧(固定ページ300)red02 | |
| | └ | (カスタム投稿タイプ「お花」>赤い花>アンスリウムの記録) |
└ | ラベンダーの記録 記事一覧(固定ページ400)blue01 | |
└ | (カスタム投稿タイプ「お花」>青い花>ラベンダーの記録) |
single-flower.phpをテーマの中に作る。
※カスタム投稿タイプ名が「flower」の場合
テンプレートの中身はこれです。↓
条件分岐で固定ページと投稿ページ両方書いてしまいます。
<?php /*Template Name:お花*/ get_header();?>
<div id="main" class="wrapper">
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if (is_page( '100' )) { //お花の記録トップページ(固定ページ)ID ?>
<?php get_template_part( 'content', 'page' ); ?>
<h2>お花の記録 記事一覧</h2>
<h3>赤い花 記事一覧</h3>
<dl class="nav-menu">
<?php
$args = array('taxonomy' => 'red','orderby' => 'slug','order' => 'ASC');
$categories = get_categories( $args );
foreach ( $categories as $category ) :
$my_posts = get_posts( $args );
echo '
<dt class="'.$category->slug.'">' . $category->name . '</dt>
';
query_posts('posts_per_page=1&post_type=flower&red=' . $category->slug);
while(have_posts()) :
the_post();
echo '
<dd><a href="'.get_the_permalink().'">'.get_the_title().'</a></dd>
';
endwhile;
wp_reset_query();
wp_reset_postdata();
endforeach; ?>
</dl>
<h3>青い花 記事一覧</h3>
<dl class="nav-menu">
<?php
$args = array('taxonomy' => 'blue','orderby' => 'slug','order' => 'ASC');
$categories = get_categories( $args );
foreach ( $categories as $category ) :
$my_posts = get_posts( $args );
echo '
<dt class="'.$category->slug.'">' . $category->name . '</dt>
';
query_posts('posts_per_page=1&post_type=flower&blue=' . $category->slug);
while(have_posts()) :
the_post();
echo '
<dd><a href="'.get_the_permalink().'">'.get_the_title().'</a></dd>
';
endwhile;
wp_reset_query();
wp_reset_postdata();
endforeach; ?>
</dl>
<?php } elseif (is_page( 'red01' )) { //アネモネの記録 記事一覧スラッグ名 ?>
<?php get_template_part( 'content', 'page' ); ?>
<div class="entry-content">
<?php query_posts( array('posts_per_page'=>-1,'order'=>'DESC','orderby'=>'date','post_type'=>'flower','red'=>'red01') );
if ( have_posts() ) :
$postmonth = false;
while ( have_posts() ) :
the_post();
if( $postmonth != get_post_time('Y') ) :
if ( $postmonth !== false ) :
endif; ?>
<h2><?php echo get_post_time('Y年'); ?> 記事一覧</h2>
<dl class="dl_archive">
<?php endif; ?>
<dt><?php the_time('Y/m/d');?></dt>
<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
<?php $postmonth = get_post_time('Y'); ?>
<?php endwhile;?>
</dl>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<?php } elseif (is_page( 'red02' )) { //アンスリウムの記録 記事一覧スラッグ名 ?>
<?php get_template_part( 'content', 'page' ); ?>
<div class="entry-content">
<?php get_template_part( 'content', 'page' ); ?>
<?php query_posts( array('posts_per_page'=>-1,'order'=>'DESC','orderby'=>'date','post_type'=>'flower','red'=>'red02') );
if ( have_posts() ) :
$postmonth = false;
while ( have_posts() ) :
the_post();
if( $postmonth != get_post_time('Y') ) :
if ( $postmonth !== false ) :
endif; ?>
<h2><?php echo get_post_time('Y年'); ?> 記事一覧</h2>
<dl class="dl_archive">
<?php endif; ?>
<dt><?php the_time('Y/m/d');?></dt>
<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
<?php $postmonth = get_post_time('Y'); ?>
<?php endwhile;?>
</dl>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<?php } elseif (is_page( 'blue01' )) { //ラベンダーの記録 記事一覧スラッグ名 ?>
<?php get_template_part( 'content', 'page' ); ?>
<div class="entry-content">
<?php query_posts( array('posts_per_page'=>-1,'order'=>'DESC','orderby'=>'date','post_type'=>'flower','blue'=>'blue01') );
if ( have_posts() ) :
$postmonth = false;
while ( have_posts() ) :
the_post();
if( $postmonth != get_post_time('Y') ) :
if ( $postmonth !== false ) :
endif; ?>
<h2><?php echo get_post_time('Y年'); ?> 記事一覧</h2>
<dl class="dl_archive">
<?php endif; ?>
<dt><?php the_time('Y/m/d');?></dt>
<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
<?php $postmonth = get_post_time('Y'); ?>
<?php endwhile;?>
</dl>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<?php } else { //上記以外 投稿ページ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php } ?>
<?php endwhile; // end of the loop. ?>
</article>
<!-- #post -->
</div>
<!-- #content -->
</div>
<!-- #primary -->
<?php get_sidebar('flower'); ?>
<?php get_footer(); ?>
参考にしたウェブサイト
ありがとうございます!
コメントを残す