Wordpress获得条款和帖子标题

时间:2013-10-03 12:49:00

标签: php wordpress custom-post-type custom-taxonomy

我正在尝试查询按其父词组分组的帖子标题:

术语名称1
职衔1 帖子标题2

术语名称2
职衔1 帖子标题2

e.t.c。

我创建了一个自定义帖子和分类。到目前为止,我的代码贯穿了每个术语名称,但我无法将帖子标题分组在其父条款下:( 我也不确定在这种情况下是否应该使用新的wp_query?

            <?php 
            $term_args = array(
                'parent' => 0,
                'orderby' => 'name',
                'order' => 'ASC',
                'hierarchical' => false,
                'hide_empty' => false
            );
            $terms = get_terms('musicians', $term_args);        

            $music_args = array(
                'post_type' => 'tf_musicians',
                'orderby' => 'title',
                'order' => 'ASC',
                'hierarchical' => false,
                'posts_per_page' => -1,             
            );
            $musicians = get_posts($music_args);    
                foreach( $terms as $term){
            echo '<div class="inner-content">';
                echo '<a name="back-point"></a>';                       
                echo '<h2 class="line-spc-sml">'.$term->name.'</h2>';                       
                echo '<ul class="inner">';  

                foreach ($musicians as $musician){  
                    if(has_term('2013','musicians',$musician->ID)) {
                        echo '<li>';                
                            echo '<a class="strong"  href=', get_permalink($musician->ID), '>', $musician->post_title, '</a>';      
                        echo '</li>';                                   
                    }
                    }

                echo '</ul>';
            echo '</div>';  
        }

提前致谢

1 个答案:

答案 0 :(得分:2)

您需要在条款循环中获得帖子

foreach ($terms as $term) {

 $music_args = array(
 'post_type' => 'tf_musicians',
 'orderby' => 'title',
 'order' => 'ASC',
 'hierarchical' => false,
 'posts_per_page' => -1,
 'your taxonony goes here' => $term->name
  );

 $musicians = get_posts($music_args);
 echo '<div class="inner-content">';
 echo '<a name="back-point"></a>';
 echo '<h2 class="line-spc-sml">' . $term->name . '</h2>';
 echo '<ul class="inner">';

 foreach ($musicians as $musician) {

 echo '<li>';
 echo '<a class="strong"  href=', get_permalink($musician->ID), '>', $musician->post_title, '</a>';
 echo '</li>';

 }

 echo '</ul>';
 echo '</div>';
 }
  

注意在中提供分类法名称''你的分类学在这里'=&gt;   $条款而─&GT;名称