我有两个自定义的帖子类型为我的网站,但我使用相同的类别/标签。
我试图创建一个category.php页面,这将允许我显示该类别内的所有项目,但在两个单独的区域,每个类型一个。
我可以在主循环中显示第一个帖子类型,但是我如何构建第二个循环来仅显示属于该类别的第二种类型的帖子?
我建议您使用WP_Query和args部分的实例,因为这两个部分都设置了所需的post_type=
<?php
//general code to get the current category id
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
//for second loop of second post type
$args1 = array(
'post_type' => 'your_post_type',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query1 = new WP_Query( $args1 );
if ( $the_query1->have_posts() ) :
while ( $the_query1->have_posts() ) : $the_query1->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();