複数の親カテゴリを記事に読み込み かつ 片方のカテゴリーのサブカテゴリー順で表示させる

条件:7かつ105カテゴリーで記事表示 表示する際 7のサブカテゴリー順に表示をさせる

 

<?php global $post; ?>

<?php
// 1. カテゴリ7のサブカテゴリーを順序通りに取得
$sub_terms = get_terms(array(
‘taxonomy’ => ‘category’,
‘parent’ => 7,
‘hide_empty’ => true,
‘orderby’ => ‘term_order’, // term_orderを使うならターム順序プラグインが必要
‘order’ => ‘ASC’,
));
?>

<ul class=”list2″>
<?php if ( ! empty( $sub_terms ) ) : ?>

<?php foreach ( $sub_terms as $term ) :

// 2. 条件:「カテゴリ7と105とこのサブカテゴリー」投稿取得
$q = new WP_Query(array(
‘posts_per_page’ => -1,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘category__and’ => array(7, 105, $term->term_id),
));

if ( $q->have_posts() ) :
while ( $q->have_posts() ) : $q->the_post(); ?>
<li class=”item”>
<ol class=”licon”>
<?php
$cfcb = get_field_object(‘iconl’);
$cfcbId = get_post_meta(get_the_ID(),’iconl’,true);
if ( $cfcb && is_array($cfcbId) ) {
foreach ( $cfcbId as $v ) {
echo ‘<li class=”ico-‘. esc_attr($v) .'”>’ . esc_html($cfcb[‘choices’][$v]) . ‘</li>’;
}
}
?>
</ol>
<a href=”<?php the_permalink(); ?>” class=”item2″><img src=”<?php the_field(‘photo01’); ?>” alt=””></a>
<h4><a href=”<?php the_permalink(); ?>”><?php the_field(‘temple’); ?></a></h4>
<h5><?php echo nl2br(esc_html(get_post_meta(get_the_ID(), ‘place’, true))); ?></h5>
<?php if ( get_field(‘pricetop’) ): ?>
<dl class=”clearfix price”>
<dt>費用目安</dt>
<dd><?php echo nl2br(esc_html(get_post_meta(get_the_ID(),’pricetop’,true))); ?></dd>
</dl>
<?php endif; ?>
<a href=”<?php the_permalink(); ?>” class=”more”>詳細を見る</a>
</li>
<?php endwhile; wp_reset_postdata();
endif;

endforeach; ?>

<?php else : ?>
<li class=”noline”>※該当する寺院情報はありません</li>
<?php endif; ?>
</ul>