使用wp_get_archives
将返回结果的预定义模板。要做到这一点,您需要自己的自定义查询和模板。由于我不知道您到底在寻找什么,我将针对您的情况提供一个基本的代码建议:
// Get a list of categories
$terms = get_terms(array(\'taxonomy\' => \'category\'));
// Run a query for each category
foreach ($terms as $term){
$args = array(
\'post_type\' => \'custom_type\',
\'posts_per_page\' => \'1\',
\'order_by\' => \'date\',
\'order\' => \'ASC\',
\'cat\' => $term->ID
);
$new_query = new WP_Query ($args);
if ($new_query->have_posts()) {
while($new_query->have_posts()){
$new_query->the_post();
// Post\'s title
the_title();
// Post\'s featured image
the_post_thumbnail(\'thumbnail\');
}
}
wp_reset_postdata();
}
这将列出每个类别中的1篇帖子,并输出特色图片及其标题。然而,如果你有很多类别,它会产生很多查询。(大约50个)。我建议您将其与缓存插件一起使用。
您还可以获取帖子列表,并在showcase中查看它们,以及它们的类别名称:
$args = array(
\'post_type\' => \'custom_type\', // Your custom post type
\'posts_per_page\' => \'8\', // Change the number to whatever you wish
\'order_by\' => \'date\', // Some optional sorting
\'order\' => \'ASC\',
);
$new_query = new WP_Query ($args);
if ($new_query->have_posts()) {
while($new_query->have_posts()){
$new_query->the_post();
the_title();
the_post_thumbnail(\'thumbnail\');
// Get a list of post\'s categories
$categories = get_the_category($post->ID);
foreach ($categories as $category) {
echo $category->name;
}
}
}
wp_reset_postdata();