我正在尝试使用WordPress中名为“案例研究”的自定义分类法显示所有类别的最新x帖子。
我已经设法输出所有类别名称作为标题链接我也在每个类别下面输出了一些帖子但它们都是相同的,我需要一种方法来过滤类别的帖子输出,详见下面的PHP评论
<?php if (have_posts()) :
$tax = 'case-studies';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms($tax, $cat_args);
foreach($categories as $category) {
echo '<p><a href="' . get_term_link( $category, $tax ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></p>';
// Post output is not working, outputs all posts using case-studies taxonomy for each category, I need a way to filter this query by category, 'category' => $category->term_id does not work, nor does 'category' => $category
$post_args = array(
'posts_per_page' => 10,
'post_type' => 'case-studies'
);
$posts = get_posts($post_args);
foreach($posts as $post) { ?>
<a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a><br/>
<?php }
echo '<p><a href="' . get_term_link( $category, $tax ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></p>';
}
endif; ?>