2017-08-02 38 views
1

我一直在使用這個循環代碼嘗試過了,它讓我從置頂文章只是一個帖子:我如何可以查詢最近5篇置頂文章WordPress的

<div id="content"> 
<ul class="disclosure table group"> 
<?php 
$sticky = get_option('sticky_posts'); 
$args = array(
    'posts_per_page' => 10, 
    'post__in' => $sticky, 
    'ignore_sticky_posts' => 1 
); 

$query = new WP_Query($args); 
if (isset($sticky[0])) { 
?> 

    <li style="text-align: justify; font-weight: 500; color: #b30404;"> 
<a href="<?php the_permalink(); ?>" style="color: #b30404;" title="<?php the_title(); ?>"><span style="font-size: 15px;"><?php the_title(); ?> </span></a> 
</li> 
<?php } ?></ul></div> 

,我希望它顯示5.我曾嘗試加入這一行的代碼,但不工作:

$sticky = array_slice($sticky, 0, 5); 

我需要我怎樣才能使這個代碼顯示5個最新的帖子(只有置頂文章)的幫助。或者給我一個我可以使用的代碼,這將是我的請求的解決方案。在此先感謝

回答

0
$sticky = get_option('sticky_posts'); 

rsort($sticky); 

$sticky = array_slice($sticky, 0, 5); 

$the_query = new WP_Query(array('post__in' => $sticky, 'ignore_sticky_posts' => 1)); 

試試這個

,並刪除此行if (isset($sticky[0])) {

查詢

if ($the_query->have_posts()) { 

    while ($the_query->have_posts()) { 

     $the_query->the_post(); 

     the_title(); 
    } 
} 
+0

仍然沒有工作後加入這個循環。或者,也許我沒有用正確的方式。請你幫忙整理一下。感謝您提供給我的代碼 –

+0

,這就是我使用它的方式。但它不起作用。它仍然顯示粘滯帖子的一個帖子,而不是5. –

+0

是的,因爲你沒有循環,嘗試編輯ans – Exprator

相關問題