我有投资组合自定义帖子类型与类别。我想使用模板来获得仅一个类别的投资组合帖子。
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => -1,
'category_name' => 'casino'
);
如果我将category_name与类似'casino'的类别slug一起使用,我必须为每个类别创建模板,但我不想要它。我想只使用一个模板,类别slug将动态放置在category_name值上。我该如何创建它?
要为自定义帖子创建类别模板:使用以下方法获取类别对象;
<?php $term = get_queried_object(); ?>
然后,您可以将此作为页面标题回显,以便您的用户知道他们正在浏览某个类别,然后在自定义查询参数中使用它。
<?php echo $term->name; ?> // shows category name
$args = array(
'post_type' => 'portfolio',
'category_name' => $term->slug
);