カスタム投稿タイプを作成し、タクソノミー(カテゴリ)やタームを新規で追加できるようにした場合の出力方法
トップページや固定ページ内にカスタム投稿のタームによる分類で出力する
function.phpへの記述でカスタム投稿タイプを「hogehoge」タクソノミーを「hogehoge_category」タームを「example」にした場合
<?php
$args=array(
'tax_query' => array(
array(
'taxonomy' => 'hogehoge_category', //タクソノミーを指定
'field' => 'slug', //ターム名をスラッグで指定する
'terms' => array( 'example' ) //表示したいタームをスラッグで指定
),
),
'post_type' => 'hogehoge', //カスタム投稿名
'posts_per_page'=> 5 //表示件数(-1で全ての記事を表示)
);
?>
<?php query_posts( $args ); ?>
<?php if(have_posts()): ?>
<?php while(have_posts()):the_post(); ?>
ページが存在する場合の指定
<p><?php the_content(); ?></p>
<?php endwhile; else: ?>
ページが存在しない場合の指定
<?php endif; ?>
<?php wp_reset_query(); ?>
デフォルトの投稿タイプの場合、投稿タイプは「post」タクソノミーは「category」となる
以下はデフォルト投稿タイプの場合の記述
<?php
$args=array(
'tax_query' => array(
array(
'taxonomy' => 'category', //タクソノミーを指定(デフォルト)
'field' => 'slug', //ターム名をスラッグで指定する
'terms' => array( 'event','campaign' ) //表示したいタームをスラッグで指定
),
),
'post_type' => 'post', //投稿名(デフォルト)
'posts_per_page'=> 5 //表示件数(-1で全ての記事を表示)
);
?>
<?php query_posts( $args ); ?>
<?php if(have_posts()): ?>
<?php while(have_posts()):the_post(); ?>
ページが存在する場合の指定
<p><?php the_content(); ?></p>
<?php endwhile; else: ?>
ページが存在しない場合の指定
<?php endif; ?>
<?php wp_reset_query(); ?>