首页 文章

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

提问于
浏览
0

好的,这是我正在尝试做的事情:

我有一个名为drink-menu的自定义帖子类型,一个名为drink-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 但是它不起作用,一切都按字母顺序显示 .

任何可以指向正确方向的Wordpress大师?我希望我已经清楚这个问题 . 谢谢 :-)

1 回答

  • 0
    • 在管理页面中按"menu_order"订购的帖子;

    • 如果你想使用query_posts(WP_Query构造函数)订购帖子,你应该使用大写的变量"orderby"的值 - > "ID" .

相关问题