简体   繁体   English

WordPress获取术语及其帖子标题

[英]Wordpress get terms and their post titles

I am trying to query for post titles grouped by their parent term: 我正在尝试查询按其父项分组的帖子标题:

Term Name 1 术语名称1
Post Title 1 帖子标题1
Post Title 2 帖子标题2

Term Name 2 术语名称2
Post Title 1 帖子标题1
Post Title 2 帖子标题2

etc 等等

I have created a custom post and taxonomy. 我创建了一个自定义帖子和分类法。 So far the code I have runs through each Term Name, but I am unable to get the Post Titles grouped under their parent terms :( I am also not sure if I should use new wp_query in this situation? 到目前为止,我已经在每个术语名称中使用了代码,但是我无法按其父术语将帖子标题分组:(我也不确定在这种情况下是否应该使用新的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>';  
        }

Thanks in advance 提前致谢

You need to get posts in the loop of terms 您需要在条款循环中获取帖子

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>';
 }

Note provide the taxonomy name in 'your taxonony goes here' => $term->name 请注意在“您的分类到这里”中提供分类名称=> $ term-> name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM