【发布时间】:2021-12-28 13:30:42
【问题描述】:
我用这段代码在functions.php中创建了一个自定义帖子类型。
function create_recipes() {
register_post_type('recipe', [
'public' => true,
'show_in_rest' => true,
'labels' => [
'name' => 'Recipes',
'add_new_item' => 'Add New Recipe',
'edit_item' => 'Edit Recipe',
'all_items' => 'All Recipes',
'singular_name' => 'Recipe',
],
'supports' => ['title', 'editor'],
'rewrite' => ['slug' => 'recipes'],
'menu_icon' => 'dashicons-media-archive',
'has_archive' => true,
'taxonomies' => array('category'),
'supports' => array( 'title', 'editor', 'author', 'thumbnail' ),
]);
}
add_action('init', 'create_recipes');
现在我正在尝试在我的前端获取/显示我在此处创建的具有不同类别的所有帖子
<?php
$recipes = new WP_Query(['post_type' => 'recipe', 'category' => '01']);
while ($recipes->have_posts()):
$recipes->the_post();
?>
<div class="sub-column">
<div class="sub-cat">
<?php the_category(); ?>
</div>
<a>
<div class="sub-thumbnail">
<?php echo the_post_thumbnail(); ?>
</div>
</a>
<div class="sub-title">
<h4><?php the_title(); ?></h4>
</div>
</div>
<?php endwhile;
?>
但我无法让它工作。现在我得到了所有不同的类别,这很好,但是具有相同类别的帖子应该直接打印在后面而不是上面的相同类别名称。
【问题讨论】: