按分类术语显示自定义帖子类型列表

时间:2013-04-30 12:58:59

标签: wordpress taxonomy custom-post-type wp-list-categories

我需要显示按术语分类的自定义帖子类型列表。这个想法是这样的:

服务清单:

<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 ,但没有成功。

  • 有人可以给我一个提示如何继续吗?

1 个答案:

答案 0 :(得分:1)

您可以将get_postsWP_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>