简体   繁体   中英

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

ok, here's what I'm trying to do:

I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.

The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...

I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:

    <?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;

    ?>

This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.

Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)

  1. Posts ordered in admin page by "menu_order";
  2. If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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