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.
任何建議如何調整上面的代碼,並使其工作?
是的,這正是我所需要的。非常感謝 –
不錯的一個!完全沒問題:) – McNab