我一直在尝试实现上述问题(没有成功)。研究了一段时间以不同的方式来实现它,但是我在某个时候陷于困境。 我创建了一个CPT(“服务”),它具有自己的分类法(“ service_category”),分为3类;并且我想显示每个自定义类别及其对应的自定义帖子类型,每种类型都在不同的。 html代码如下:
<!-- .grid_4 .service-box start 1 -->
<section class="grid_4 service-box">
<div class="service-info">
<div class="service-info-icon">
<i class="service-icon icon-write"></i>
</div>
<div class="service-info-title">
<a href="">
<h6>Copywriting</h6>
</a>
<span>Great copy is very important</span>
</div>
</div>
<ul><li>
There are many variations of passages of Lorem
Ipsum available, but the majority have suffered
alteration in some form.
</section><!-- .grid_4 .service-box end -->
<!-- .grid_4 .omega .service-box start -->
<section class="grid_4 service-box">
<div class="service-info">
<div class="service-info-icon">
<i class="service-icon icon-mac"></i>
</div>
<div class="service-info-title">
<a href="">
<h6>Responsive design</h6>
</a>
<span>It looks good on mobile too</span>
</div>
</div>
<ul>
<li>display CPT title here</li>
</ul>
</section><!-- .grid_4 .omega .service-box end -->
我尝试了以下代码,但未达到预期效果。
<?php $cat_args = array(
'taxonomy' => 'service_category',
'orderby' => 'slug',
'order' => 'ASC'
);
$cats = get_categories($cat_args); // passing in above parameters
foreach ($cats as $cat) : // loop through each cat
$cpt_query_args = new WP_Query( array(
'post_type' => 'service',
'service_sections' => $cat->name
)
);
if ($cpt_query_args->have_posts()) :
while ($cpt_query_args->have_posts()) : $cpt_query_args->the_post(); ?>
<!-- .grid_4 .service-box start -->
<section class="grid_4 service-box">
<?php echo '<h3>' . $cat->name . '</h3>'; ?>
<div class="service-info">
<div class="service-info-icon">
<i class="service-icon icon-mac"></i>
</div>
<div class="service-info-mac">
<!--<div id="<?php //echo $cat->slug; ?>">-->
<!--<div class="<?php //echo $cat->slug; ?>" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>-->
<h6> <?php the_title(); ?></h6>
<?php the_content(); ?>
<!--</div>-->
<!--</div>-->
</div>
</div>
</section>
</section>
<?php endwhile;
endif;
wp_reset_query();
endforeach; ?>
任何帮助将不胜感激。谢谢!!