我有兩種帖子類(lèi)型,常規(guī)帖子和自定義帖子類(lèi)型。一切正常,我只顯示 5 個(gè)帖子。一個(gè)是完整的帖子,四個(gè)是摘錄。我的問(wèn)題是摘錄顯示的是最新的帖子,與帖子類(lèi)別無(wú)關(guān)。我想顯示兩個(gè)帖子和兩個(gè)自定義帖子類(lèi)型。$args = array( 'post_type' => array( 'post', 'tutorial' ),);$query = new WP_Query( $args );if ( $query->have_posts() ) : $count = 0; while ( $query->have_posts() ) : $query->the_post(); if ( $count == 0 ) { ?> <h2><?php the_title(); ?></h2> <?php the_content(); $count ++; } else { ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); } endwhile;endif;wp_reset_postdata();?>預(yù)期的輸出應(yīng)該是最新的帖子作為完整的帖子,因?yàn)樗F(xiàn)在正在工作。然后它應(yīng)該顯示帖子類(lèi)型帖子的兩個(gè)最新帖子和帖子類(lèi)型教程的兩個(gè)最新帖子。
1 回答

開(kāi)滿(mǎn)天機(jī)
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
基本上你只需要按posttype排序
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => 'post_type',
'order' => 'ASC',
);
如果您想將日期排序保留為次要排序,這應(yīng)該可以工作(未測(cè)試)。
$args = array(
'post_type' => array( 'post', 'tutorial' ),
'orderby' => array ('post_type' => 'ASC', 'order' => 'DESC' ),
);
有關(guān)更多信息,請(qǐng)查看WP_Query 文檔
請(qǐng)記住,如果您有 5 個(gè)帖子比您的任何教程都更新,則不會(huì)顯示任何帖子。為了保證 3 個(gè)帖子和 2 個(gè)教程,您需要wp_query使用posts_per_page參數(shù)將代碼分成 2 個(gè)循環(huán)。
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)
0/150
提交
取消