1

我想就 WordPress 置顶帖功能遇到的问题寻求帮助。

我不知道如何使棍子贴在循环的开头。我有一个类似于他的循环:

<?php query_posts('cat=10&posts_per_page=3');?>  
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?> 

我希望它像这样工作:

  • 置顶帖
  • 普通岗位
  • 普通岗位

代替:

  • 普通岗位
  • 置顶帖
  • 普通岗位

谢谢您的帮助!

4

2 回答 2

5

我的解决方案在这里http://codex.wordpress.org/Class_Reference/WP_Query

我做了两个查询,在这种情况下我没有使用分页也许可以帮助

    $sticky = get_option( 'sticky_posts' );
    $args_nonsticky = array(
        'showposts'     => -1,
        'post__not_in' => $sticky
    );
    $args_sticky = array(
        'posts_per_page' => -1,
        'post__in'  => $sticky
    );

    $the_query_sticky = new WP_Query($args_sticky); 
    $the_query_nonsticky = new WP_Query($args_nonsticky);

    if(!$the_query_sticky->have_posts() && !$the_query_nonsticky->have_posts()){
        //echo '<h1>NO POSTS FOUND</h1>';
    }else{              

    if ( $sticky[0] ) {
    while ($the_query_sticky->have_posts()) : $the_query_sticky->the_post(); 
      //sticky if so...
    endwhile;
    }

    while ($the_query_nonsticky->have_posts()) : $the_query_nonsticky->the_post(); 
        // non sticky
    endwhile;
}
于 2012-01-12T09:43:06.980 回答
2

我在我的演示网站上对此进行了测试。并且默认顺序应该是: - 粘性 - 普通 - 普通 默认顺序是 NOT - 普通 - 粘性 - 普通

我建议用其他主题来测试它,比如二十个。从那里可能是基本的调试检查: http ://codex.wordpress.org/Sticky_Posts

于 2011-09-07T21:16:25.357 回答