Advanced Custom Fieldsのカスタムフィールドを利用してカスタム投稿タイプのタームで記事を絞り込む例
tax_queryのrelationをANDにしてブランドとタイプで絞り込み、WP_Queryでループさせる
カスタム投稿タイプ:items
カスタム投稿タイプのタクソノミー:items_category
カスタムフィールド:00brand , item_type , 02item-image1(メイン画像)
<h3>関連商品</h3>
<div id="recommend">
<?php
$brand = get_field('00brand');
$type = get_field('item_type');
$args = array(
'post_type' => 'items', //カスタム投稿タイプ名
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'items_category',
'field' => 'slug',
'terms' => $brand,
),
array(
'taxonomy' => 'items_category',
'field' => 'slug',
'terms' => $type,
),
),
'numberposts' => 12, //12件表示(デフォルトは5件)
'orderby' => 'abc', //ランダム表示 rand
'post__not_in' => array($post->ID) //表示中の記事を除外
);
?>
<p><?php echo $type; ?></p>
<div class="item">
<div class="itemList inner clearfix">
<?php $recommend = new WP_Query( $args ); ?>
<?php if($recommend -> have_posts()): while($recommend -> have_posts()): $recommend -> the_post(); ?>
<?php
$attachment_id1 = get_field('02item-image1');
$size1 = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$image1 = wp_get_attachment_image_src( $attachment_id1, $size1 );
$attachment_id1f = get_field('02item-image1');
$size1f = "large"; // (thumbnail, medium, large, full or custom size)
$image1f = wp_get_attachment_image_src( $attachment_id1f, $size1f );
$attachment = get_post( get_field('02item-image1') );
$alt1 = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title1 = $attachment->post_title;
?>
<div class="itemBox4"><a href="<?php the_permalink(); ?>"><img src="<?php echo $image1f[0]; ?>" alt="<?php echo $alt1; ?>" title="<?php echo $image_title1; ?>"><h4><?php echo nl2br(post_custom('01model')); ?></h4><p><?php echo nl2br(post_custom('03price')); ?></p></a></div>
<?php endwhile; ?>
</div><!--/itemList-->
</div><!--/item-->
<?php else : ?>
<p>関連アイテムはまだありません。</p>
</div><!--/itemList-->
</div><!--/item-->
<?php endif; wp_reset_postdata(); ?>
</div><!--/#recommend-->