前回の「カスタム投稿タイプとカスタムタクソノミーの追加」の続きですが、今回はカスタム投稿タイプで投稿した記事の表示方法についてです。
シングルテンプレート(single.php)で表示するには
「カスタム投稿タイプ」ならテーマのsingle.phpファイルをコピーしてテーマディレクトリ内にsingle-[post-type].phpを作成する方法が簡単です。例)single-store.php
ただし、コピー元のファイル(single.php)内にテンプレートタグ(the category/the tags)で
カテゴリ・タグを表示している場合には「投稿」のカテゴリ・タグが表示されてしまいますので、
カスタム分類のテンプレートタグ(get_the_term_list)に置き換えてください。
参照)カスタム分類をリンク付で表示・・テンプレートタグget_the_term_list
例)カスタム分類(area)をカンマ区切りで表示
<?php echo get_the_term_list( $post->ID, 'area', '', ', ', '' ); ?>
例)カスタム分類(product)をリストで表示
<?php echo get_the_term_list( $post->ID, 'product', '<ul><li>','</li><li>','</li></ul>' ); ?>
ページテンプレートで表示する場合
ページテンプレートで表示する場合には「固定ページ」用のテンプレートを作成。
「固定ページ」新規作成>テンプレートを選択して読み込みます。
ページテンプレートでカスタム投稿タイプの記事を5件表示(ページナビ付)
例)カスタム投稿タイプ=store カスタム分類=area,product
<?php /* Template Name: Custom_Post_Types */ ?>
<?php get_header(); ?>
<div id="content">
<div class="custom-post_content">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => array('store'),'posts_per_page' => 5,'paged' => $paged ));
if (have_posts()) :while (have_posts()) : the_post();
?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<ul class="custom-taxonomies">
<li><?php echo get_the_term_list( $post->ID, 'area', 'エリア:', ', ', '' ); ?></li>
<li><?php echo get_the_term_list( $post->ID, 'product', '取扱商品:', ', ', '' ); ?></li>
</ul>
<p><?php the_content('続きを読む»'); ?></p>
<?php endwhile;
endif; ?>
<div class="navigation">
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { ?>
<div class="left"><?php previous_posts_link('前へ') ?></div>
<div class="right"><?php next_posts_link('次へ') ?></div>
<?php } ?>
</div>
<?php wp_reset_query(); ?>
</div><!-- end custom-post_content -->
</div><!-- end content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
表示サンプル画像・・ページテンプレートで表示。※テスト投稿2件なので1件(‘posts_per_page’ => 1)で表示

アーカイブで表示する場合(テンプレート)
カスタム投稿タイプのアーカイブをテンプレートで表示するには、single-[post-type].php と同様に archive-[post-type].phpを使って表示できます。
'has_archive' => true, /* アーカイブ生成 */
この場合functions.phpに設定した「カスタム投稿タイプ」のregister_post_type() に上記の引数の追加が必要になります。
参照)カスタム投稿タイプのアーカイブ用テンプレート・・Wordpress.org フォーラム
functions.php
/* カスタム投稿タイプの追加 */
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'store', /* 投稿タイプ名 */
array(
'labels' => array(
'name' => __( '販売店' ), /* 管理画面の表示名 */
'singular_name' => __( '販売店' )
),
'public' => true,
'menu_position' => 5,
'has_archive' => true, /* アーカイブ生成 */
/* 投稿画面の表示項目 */
'supports' => array('title','editor','thumbnail','custom-fields','excerpt','author','page-attributes')
)
);
}
カスタム投稿タイプの一覧(リンク付)をリストで5件表示するには
例)サイドバーに表示する場合
<div class="widget">
<h3>販売店一覧(カスタム投稿タイプ)</h3>
<div class="widget_body">
<ul>
<?php
query_posts('post_type=store' , array('posts_per_page' => 5));
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; wp_reset_query();
?>
</ul>
</div>
</div>
上記の他にもカスタムメニュー(外観>メニュー)にテンプレートを保存してウィジェットで表示する方法、また、プラグイン「Custom Post Type List Widget」が簡単で便利です。
カスタム分類の一覧(リンク付)をリストで表示するには
例)サイドバーに表示する場合・・参照)テンプレートタグ/wp list categories
<div class="widget">
<h3>カスタム分類(エリア)</h3>
<div class="widget_body">
<ul>
<?php wp_list_categories('taxonomy=area&title_li='); ?>
</ul>
</div>
</div>
ピンバック: [Ktai Style]カスタム投稿タイプ&single.php独自テンプレート利用時の携帯挙動がおかしい « とある無職の警備記録<WordPress>
ピンバック: カスタムフィールド使用いろいろまとめ – : 静岡 SOHO Web designer 生活