开发者

Making a sticky post the first post in the loop - WordPress

开发者 https://www.devze.com 2023-04-03 00:32 出处:网络
I would like to get help with a issue I\'m having with the WordPress sticky posts function. I cant figure it out how to make the stick post stick to 开发者_开发问答the beginning of the loop. I have a

I would like to get help with a issue I'm having with the WordPress sticky posts function.

I cant figure it out how to make the stick post stick to 开发者_开发问答the beginning of the loop. I have a loop similar to his:

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

And I would like it to work like this:

  • Sticky post
  • Ordinary post
  • Ordinary post

Instead for:

  • Ordinary post
  • Sticky post
  • Ordinary post

Thanks for the help!


My solution here http://codex.wordpress.org/Class_Reference/WP_Query

I made two queries, in this case I'm not using pagination maybe this can help

    $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;
}


I tested this on my Demo site. And the default order should be: - sticky - ordinary - ordinary The default order is NOT - ordinary - sticky - ordinary

I recommend testing it with other theme's, like twentyten. From there it is probably basic debugging check this: http://codex.wordpress.org/Sticky_Posts

0

精彩评论

暂无评论...
验证码 换一张
取 消