【问题标题】:Wordpress get terms and their post titlesWordpress 获取术语及其帖子标题
【发布时间】:2013-10-10 04:18:04
【问题描述】:

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

术语名称 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>';  
        }

提前致谢

【问题讨论】:

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


    【解决方案1】:

    您需要在术语循环中获取帖子

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

    注意在'your taxonony go here'中提供分类名称=> $term->名称

    【讨论】:

    • 非常感谢!我真的把头发拉了出来。那么你有'你的分类在哪里',我猜那是'类别'参数?
    • @user1575949 注册自定义帖子类型及其类别后,您可以从register_taxonomy()函数的第一个参数中获取分类名称
    • +1 我现在在法典中看到它Taxonomy parameters
    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2020-02-14
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多