简体   繁体   English

WordPress循环-以自定义分类顺序显示自定义分类术语中的帖子

[英]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. 我有一个自定义的帖子类型,称为Drinks-menu,一种分类法,称为Drinks-menu-categories,以及一个页面模板,名为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. 您可以看到我尝试了taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC但它不起作用,所有内容taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC字母顺序显示。

Any Wordpress gurus who can point me in the right direction? 任何可以帮助我指出正确方向的Wordpress专家? I hope I've been clear about the issue. 我希望我对这个问题很清楚。 Thanks :-) 谢谢 :-)

  1. Posts ordered in admin page by "menu_order"; 在管理页面中按“ 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". 如果您要使用query_posts(WP_Query构造函数)订购帖子,则应使用大写字母->“ ID”的变量“ orderby”的值。

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

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