下面的代码返回为自定义分类法"music_categories"(摇滚、爵士、说唱等)找到的所有tax_terms,然后将自定义帖子类型"music"的所有帖子排列在相应的标题下。
但我需要更多的过滤。后类型音乐有第二个自定义分类法,称为"portfolio",包含术语"PortfolioOne"、"PortfolieTwo"answers"PortfoliaThree"。
在下面的代码开始运行之前,我需要能够选择一个投资组合。
输出如下:
爵士乐
邮政1号、邮政4号、邮政10号
岩石
邮政4号、邮政5号、邮政6号
Blues
邮政4号、邮政10号
等等。。。
$post_type = 'music';
$tax = 'music_categories';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<h1 class="taxonomy-heading">'. $tax_term->name . '</h1>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
// a post with given taxonomy (heading, paragraphs, images etc)
<?php
endwhile;
}
wp_reset_query();
}
}
?>
我在脑海中把这种方式复杂化了很多。我在$args数组中添加了一行,瞧!
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'portfolios' => 'portfolio-two',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
现在,我从帖子类型的音乐中得到了标记为公文包二(分类法)的所有帖子的列表。它们被列在它们所属的流派分类之下。