2017-08-02 33 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

相关问题