カスタム投稿タイプの分類別一覧を年度毎に固定ページに表示

以下の事をしようとしています。(未来のド忘れした自分に向けて書いてます)

  • 固定ページにカスタム投稿タイプ【hana】に投稿したの分類別一覧を年度別に自動抽出
  • 「カスタム投稿タイプ【hana】」への投稿はカスタム分類(タクソノミー)で分ける
  • 分類は赤(red)、青(blue)の2種類とします。
  • 上記をすべて1つのテンプレートにまとめる

あとで管理する時にテンプレートが分かれていると、物忘れの激しい私は混乱するのでちょっと長くなるけどまとめてしまおうという魂胆です。

カテゴリーやタグを使わずにカスタム分類を使用する理由は、
カテゴリーは別の目的で使用中だった事と、タグは管理が面倒なのでチェックボックスが使えるカスタム分類を採用します。

下準備

  1. カスタム投稿タイプ「お花」を作る。(Post Type Slugは「flower」)
  2. 1で作った投稿タイプに下記2つのタクソノミーをつくります。※()内はTaxonomy Slug
    赤い花(red)
    青い花(blue)
    この時、Edit Taxonomiesの画面で赤い花と青い花の階層を「True」にしておけば、記事投稿時にチェックボックスを使えるようになります。
  3. カスタム分類を追加すると、↓こんな感じになります。
  4. カスタム分類の「赤い花」と「青い花」にそれぞれスラッグをいくつか追加。
  5. 新規追加画面はこんな感じになります。

テンプレートを作る

↓こんな構成になるテンプレートを作ります。
※オレンジ文字は固定ページのID番号。ピンク文字はスラッグ名。

お花の記録(固定ページ100
アネモネの記録 記事一覧(固定ページ200red01
(カスタム投稿タイプ「お花」>赤い花>アネモネの記録)
アンスリウムの記録 記事一覧(固定ページ300red02
(カスタム投稿タイプ「お花」>赤い花>アンスリウムの記録)
ラベンダーの記録 記事一覧(固定ページ400blue01
 (カスタム投稿タイプ「お花」>青い花>ラベンダーの記録)

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(); ?>

参考にしたウェブサイト

ありがとうございます!


Comments

“カスタム投稿タイプの分類別一覧を年度毎に固定ページに表示” への13件のフィードバック

  1. Johna726のアバター
    Johna726

    Really enjoyed reading ur blog. afdeeaeedbad

    1. えび子のアバター
      えび子

      Thank you! I’m happy.

  2. Johna268のアバター
    Johna268

    I loved your post.Much thanks again. fekedcefekde

    1. えび子のアバター
      えび子

      Thank you for saying so.
      I am very happy.

  3. Smithb580のアバター
    Smithb580

    I appreciate, cause I found just what I was looking for. You have ended my 4 day long hunt! God Bless you man. Have a nice day. Bye bkdegdakebekkgcc

    1. えび子のアバター
      えび子

      Thank you.
      I am very pleased that I was able to help you.
      Have a nice day for you too!

  4. Johnc91のアバター
    Johnc91

    Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate? eeakdecekkbd

  5. Smithk568のアバター
    Smithk568

    I’m hunting for web sites that contain fantastic recommendations on what’s in fashion and just what top rated makeup products is.. ckbgcbegeedeffab

  6. Smithk956のアバター
    Smithk956

    Excellent blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my website loaded up as quickly as yours lol fddddeddcddkeeee

  7. のアバター
    匿名

    I like this post, enjoyed this one thankyou for posting . dcbddbdedfkc

    1. えび子のアバター
      えび子

      Thank you
      I am honored.

  8. のアバター
    匿名

    Hi! This is my first visit to your blog! We are a cagaakdfdeedeebc

    1. えび子のアバター
      えび子

      I do not know what ‘cagaakdfdeedeebc’ is but Thank you!

コメントを残す