我需要显示按术语分类的自定义帖子类型列表。这个想法是这样的:
服务清单:
<ul>
<li>
<a href="#">Webdesign (taxonomy term)</a>
<ul>
<li>Business Website A (custom post type from this taxonomy term)</li>
<li>Business Website B (custom post type from this taxonomy term)</li>
<li>Business Website C (custom post type from this taxonomy term)</li>
</ul>
</li>
<li>
<a href="#">Illustration (taxonomy term)</a>
<ul>
<li>Business Illustration A (custom post type from this taxonomy term)</li>
<li>Business Illustration B (custom post type from this taxonomy term)</li>
<li>Business Illustration C (custom post type from this taxonomy term)</li>
</ul>
</li>
<li>Other service without taxonomy (if has no taxonomy, show in first level)</li>
</ul>
我已经尝试了 wp_list_pages 和 wp_list_categories ,但没有成功。
答案 0 :(得分:1)
您可以将get_posts
或WP_Query
与分类参数一起使用:
http://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
get_posts
的示例:
<ul>
<li>
<a href="#">Webdesign</a>
<ul>
<?php $items = get_posts( array(
'post_type' => 'my_post_type',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'webdesign'
)
)
) );
if( $items ){
foreach ( $items as $item ){
setup_postdata( $item );
echo '<li>' . get_the_title( $item->ID ) . '</li>';
}
} ?>
</ul>
</li>
</ul>