我的网站有两种自定义帖子类型,但两者都使用相同的类别/标签。
我正在尝试创建一个category.php页面,该页面允许我显示该类别中的所有项目,但是在两个单独的区域中,每种类型一个。
我可以获得要在主循环中显示的第一个帖子类型,但是如何构建第二个循环以仅显示该类别中第二种类型的帖子?
答案 0 :(得分:0)
我建议您使用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();