繁体   English   中英

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

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

好的,这就是我想要做的:

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

任何可以帮助我指出正确方向的Wordpress专家? 我希望我对这个问题很清楚。 谢谢 :-)

  1. 在管理页面中按“ menu_order”排序的帖子;
  2. 如果您要使用query_posts(WP_Query构造函数)订购帖子,则应使用大写字母->“ ID”的变量“ orderby”的值。

暂无
暂无

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

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