2014-04-02 93 views
0

在單個帖子頁面上,我有一個側欄顯示最多三個其他相關帖子。我如何排除粘滯帖子和當前帖子?排除當前和粘滯帖子

我知道如何排除Current post以及如何通過在WP_Query中使用post_not_in來排除Sticky Posts,請參閱下面的代碼示例。但我想你在同一個查詢中不能使用兩次post__not_in。有什麼建議麼?

$current_post_ID = get_the_ID(); 

     $args = array(
      'post_type' => 'post', 
      'post_status' => 'publish', 
      'order' => 'DESC', 
      'orderby' => 'date', 
      'posts_per_page' => 3, 
      'post__not_in' => get_option('sticky_posts') 
      'post__not_in' => array($current_post_ID) 
     ); 
+0

使用這裏解釋的解決方案:[WordPress的Stackexchange論壇] [1]。感謝所有的答覆! [1]:http://wordpress.stackexchange.com/questions/140000/exclude-current-and-sticky-post?noredirect=1#comment200219_140000 –

回答

0

post__not_in陣列()和使用POST ids(只數)。 沒有必要使用兩次,使用這樣的:

//First build the array of excluded posts $excluded_posts = array_push(get_option('sticky_posts') , $current_post_ID);

//Then use it in the option 'post__not_in' => array($excluded_posts)

注意array_push() PHP函數,這將在目前的職位添加到置頂文章數組的結尾,然後傳遞給'post__not_in'選項。

0
<?php 

         $sticky =array(get_option('sticky_posts')); 

         // (add post id on sticky_posts option like ex. 485,458,256) 

         $current_post_ID = get_the_ID(); 
         array_push($sticky,$current_post_ID); 

         $args = array(
          'post_type' => 'post', 
          'post_status' => 'publish', 
          'order' => 'DESC', 
          'orderby' => 'date', 
          'posts_per_page' => 3, 
          'post__not_in' => array($sticky) 
         ); 
         query_posts($args); 

         while (have_posts()) : the_post(); ?> 

        <!-- add your code which to display hear --> 


        <?php 
         endwhile; 
         wp_reset_query(); 
        ?>