一种方法是运行以下两个自定义查询:
$normal_post_args = array(
\'post_type\' => \'post\',
\'category_name\' => \'hobby\'
);
$normal_posts_query = new WP_Query( $normal_post_args );
$movie_post_args = array(
\'post_type\' => \'movie\',
\'category_name\' => \'hobby\'
);
$movie_posts_query = new WP_Query( $movie_post_args );
echo "Posts (" . $normal_posts_query->found_posts . ")";
echo "Movies (" . $movie_posts_query->found_posts . ")";
这应该会让你
Posts (5)
和
Movies (3)
. 这是一个起点。然后,只需将其包装到函数中并在模板文件中引用即可。
<小时>Update: 根据您的评论,我能想到的最好的方法是创建您自己的类别列表,而不是使用默认的WP列表。
类似这样:
$categories = get_terms( \'category\');
if($categories):
echo "<ul>";
foreach($categories as $cat):
$cat_posts = get_posts(\'post_type=movie&category=\' . $cat->term_id . \'&numberposts=-1\');
if($cat_posts):
echo "<li>" . $cat->name;
$count = count($cat_posts);
echo " ($count)";
echo "</li>";
endif;
endforeach;
echo "</ul>";
endif;