douzai1074
2015-02-27 21:29
浏览 96
已采纳

Wordpress posts_per_page无效

So, I tried all the "add_filter" things on the whole web to get this right but it doesn't work! It always shows me more than 1 post. What's wrong with my code? Latest WP-Version 4.1.1 and no plugins installed.

Here is the code:

<?php

    $sticky = get_option('sticky_posts');

    if ( !empty($sticky) ) {

    $args = array(
        'post__in' => $sticky,
        'orderby' => 'date',
        'order' => 'ASC',
        'posts_per_page' => 1
    );

    $slider_query = new WP_Query( $args );

    while ( $slider_query->have_posts() ) {             
        $slider_query->the_post();
    ?>

    <div>
       <!-- here we go -->
    </div>

<?php

        }
    }

?>

图片转代码服务由CSDN问答提供 功能建议

所以,我尝试了整个网络上的所有“add_filter”事情来做到这一点,但它不起作用 ! 它总是向我展示超过1个帖子。 我的代码出了什么问题? 最新的WP-Version 4.1.1并没有安装插件。

以下是代码:

 &lt;?php 
 
  $ sticky = get_option('sticky_posts'); 
 
 if(!empty($ sticky)){
 
 $ args = array(
'post__in'=&gt; $ sticky,
'orderby'=  &gt;'日期',
'顺序'=&gt;'ASC',
'possouts_per_page'=&gt; 1 
); 
 
 $ slider_query = new WP_Query($ args); 
 
 while  ($ slider_query-&gt; have_posts()){
 $ slider_query-&gt; the_post(); 
?&gt; 
 
&lt; div&gt; 
&lt;! - 这里我们去 - &gt; \  n&lt; / div&gt; 
 
&lt;?php 
 
} 
} 
 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshang6790 2015-02-27 22:22
    已采纳

    Okay, I got it. This parameter is missing. Wordpress ignores "posts_per_page" by default for sticky posts.

    $args = array(
        'post__in' => $sticky,
        'orderby' => 'date',
        'order' => 'ASC',
        'posts_per_page' => 1,
        'ignore_sticky_posts' => 1
    );
    
    点赞 打赏 评论
  • dongzou7134 2015-02-27 21:40

    Wordpress doesn't play nice with posts_per_page and wp_query. To fix it you have to use get_query_var, like so:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    

    And then in your wp_query() you do:

    $args = array(
        'post__in' => $sticky,
        'orderby' => 'date',
        'order' => 'ASC',
        'posts_per_page' => 1,
        'paged' => $paged
    );
    
    点赞 打赏 评论

相关推荐 更多相似问题