dongshilve4392 2013-08-10 12:35
浏览 44
已采纳

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

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条回答 默认 最新

  • dpeqsfx5186 2013-08-10 16:09
    关注
    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".
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功