簡體   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