2013-08-23 41 views
1

我有這個疑問:上方顯示置頂文章,然後顯示其他職位

<?php $wp_query = array(
    'post__not_in' => array(4269), 
    'post_type' => 'whatson', 
    'exclude' => '4269', 
    'posts_per_page' => 5, 
    'order' => 'ASC', 
    'orderby' => 'date', 
    'post_status' =>array('future','published')); 
?> 

它目前表示爲了即將到來的職位......我該怎麼讓頂部的置頂文章,然後顯示它下面的其他帖子?

例如,如果兩個帖子被標記爲粘性,那麼他們將顯示在頂部,然後其他3個帖子將是即將到來的帖子。

回答

1

我有一個類似的問題而回,並制定了以下解決方案,您可以得到它NY多個循環。這將告訴你如何輸出最多5個帖子,粘性頂部。您必須對參數數組進行自己的調整,但這應該指向正確的方向。

這是一個確定事實上顯示了多少粘性帖子的問題,從5中減去該數字,然後顯示非粘性帖子的餘額。

<?php 

function define_loop_args($present_cat, $sticky_toggle = 0) { 

    /*the total number of posts to display*/ 
    $this->maxNum = 5; 

    $loop_args = array(
     'cat' => $present_cat, 
    ); 

    if ($sticky_toggle == TRUE) { 
     $loop_args['post__in'] = get_option('sticky_posts'); 
     $loop_args['posts_per_page'] = $this->maxNum; 
    } else { 
     $loop_args['post__not_in'] = get_option('sticky_posts'); 
     $loop_args['posts_per_page'] = ((int)($this->maxNum) - (int)($this->sticky_count)); 
    } 

    $this->loop_args = $loop_args; 
} 


    ?> 
<ul class="no-children"> 
    <?php 

    /* 
    * STICKY 
    *output sticky posts first 
    */ 

    $this->define_loop_args($catID, 1); 
    /* 
    *count the number of sticky posts displayed, in order to calculate how many non-sticky posts to output next 
    */ 
    $sticky_count = 0; 

    // The Query 
    $the_query = new WP_Query($this->loop_args); 

    // The Loop 
    while ($the_query->have_posts()) : $the_query->the_post(); 

     ?> 
     <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> 
     <?php 

     $sticky_count++; 

    endwhile; 
    // End The Loop 

    // Reset Post Data 
    wp_reset_postdata(); 


    $this->sticky_count = $sticky_count; 

    /* 
    * NON-STICKY 
    *output non-sticky posts next 
    */ 

    $this->define_loop_args($catID); 

    $the_query = new WP_Query($this->loop_args); 

    while ($the_query->have_posts()) : $the_query->the_post(); 
     ?> 

     <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> 

    <?php 

    endwhile; 
    // End The Loop 

    // Reset Post Data 
    wp_reset_postdata(); 

    ?> 
</ul> 
0

如下

<?php 
    $sticky = get_option('sticky_posts'); 

    $args_ordinary = array(
     'post__not_in' => array(4269,$sticky), 
     'post_type' => 'whatson', 
     'exclude' => '4269', 
     'posts_per_page' => 3, 
     'order' => 'ASC', 
     'orderby' => 'date', 
     'post_status' =>array('future','published')); 

    $args_sticky = array(
      'posts_per_page' => -1, 
      'post__in' => $sticky, 
      'posts_per_page' => 2, 
      'post_type' => 'whatson' 
     ); 

    query_posts($args_sticky); 
    if (have_posts()): ?> 
    <?php while (have_posts()) : the_post(); ?> 
     //sticky post 
    <?php endwhile; ?> 
    <?php endif; ?> 

    // Now Ordinary Posts 
    query_posts($args_ordinary); 
    if (have_posts()): ?> 
    <?php while (have_posts()) : the_post(); ?> 
     //ordinary post 
    <?php endwhile; ?> 
    <?php endif; ?> 
+0

@JohtiKannan好的謝謝。是否有可能只用這種方式顯示總共5個帖子,不管它是由粘性帖子還是非粘性帖子組成? – Rob

+0

@Rob'posts_per_page'是可選的,是的,它首先列出粘性帖子,然後在其中列出其他功能,併發布後 –

+0

@JohtiKannan它並不總是肯定會有任何粘性帖子,用戶可能不希望顯示任何。所以我不認爲它可以與兩個循環一起工作,它必須在一個循環中進行,因此它可以計算總帖子數量......不僅僅是計算粘性數據,而是計算普通帖子。 – Rob