Wordpress循环-按层次顺序显示来自自定义分类术语的帖子


Wordpress Loop - Show posts from custom taxonomy terms in heirarchical order

好吧,下面是我要做的:

我有一个自定义的帖子类型,叫做饮料菜单,一个分类法,叫作饮料菜单类别,还有一个页面模板,叫做template-dinks-menu.php。

该分类法有一系列具有继承性的术语——葡萄酒,子代为白色和红色;啤酒,与儿童皮尔森纳,斯托特等…

我想使用一个循环来显示这些术语中的所有帖子,其顺序与管理员中的顺序相同。这是我目前掌握的代码:

    <?php
    $post_type = 'drinks-menu';
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
    foreach( $taxonomies as $taxonomy ) : 
        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy );
        foreach( $terms as $term ) : 
            echo '<h1>'.$term->name.'</h1>';
            if ( $term->description !== '' ) { 
                echo '<div class="page_summary">'. $term->description .'</div>';
            }
            echo '<br>';
            $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
            if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
                ?>
                <a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
                <?php
                if( get_field( "price" ) ): ?>
                    <?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
                <?php endif;
                echo '<em>'; the_excerpt(); echo '</em><br><br>';
            endwhile; endif;
        endforeach;
    endforeach;
    ?>

这可以很好地将条款中的所有帖子放到页面上,但不是按顺序。你可以看到我试过taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC,但它不起作用,所有内容都按字母顺序显示。

有谁能为我指明正确的方向?我希望我已经清楚这个问题了。感谢:-)

  1. 在管理页面中按"menu_order"排序的帖子
  2. 若要使用query_posts(WP_query构造函数)对帖子进行排序,则应使用大写的变量"orderby"的值->"ID"