2012-09-09 69 views
3

我正在剪裁我的WordPress主題,我想在我的WordPress主頁頂部添加最新的2個貼子。對於我使用下面的代碼:在WordPress主頁上顯示最新的2個貼子

<div class="trending-right"> 
    <?php 
    $sticky = get_option('sticky_posts'); // Get all sticky posts 
    rsort($sticky); // Sort the stickies, latest first 
    $sticky = array_slice($sticky, 0, 2); // Number of stickies to show 
    query_posts(array('post__in' => $sticky, 'caller_get_posts' => 1)); // The query 

    if (have_posts()) { while (have_posts()) : the_post(); ?> 
    <div class="trend-post"> 
    <div class="thumb"><?php the_post_thumbnail(array(150,100)); ?></div> 
    <div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div> 
    </div> 
    <?php endwhile;?> 
    <?php } else { echo ""; }?> 

</div> 

現在代碼工作確定並顯示最新的2篇置頂文章然而,它也將刪除首頁列出的所有其他職位,只顯示那些2篇置頂文章。我試圖用new WP_Query替換query_posts,但在這種情況下,它顯示所有粘性帖子,而不是隻顯示2.

任何建議如何調整上面的代碼,並使其工作?

回答

3

看着你的代碼,我假設你剛剛向我們展示了粘滯循環,並且還有另一個查詢來顯示模板中其他地方的其他帖子?你應該使用wp_reset_query();在您的自定義查詢之後,這是Codex中的條目;

Wordpress - resetting custom query

+0

是的,這正是我所需要的。非常感謝 –

+0

不錯的一個!完全沒問題:) – McNab

相關問題