Wordpress 类别存档:单独的 post 类型的单独循环
Wordpress Category archive: Separate loops for separate post types
我的网站有两种自定义 post 类型,但我对两者使用相同的 Categories/Tags。
我正在尝试创建一个 category.php 页面,该页面将允许我显示该类别中的所有项目,但在两个不同的区域中,每种区域一个。
我可以让第一个 post 类型显示在主循环中,但是我如何构建第二个循环以仅显示属于该类别的第二个类型中的 post ?
我建议您使用 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();
我的网站有两种自定义 post 类型,但我对两者使用相同的 Categories/Tags。
我正在尝试创建一个 category.php 页面,该页面将允许我显示该类别中的所有项目,但在两个不同的区域中,每种区域一个。
我可以让第一个 post 类型显示在主循环中,但是我如何构建第二个循环以仅显示属于该类别的第二个类型中的 post ?
我建议您使用 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();