Support » Fixing WordPress » how to: if no sticky post selected, return none

  • Resolved alsd

    (@alsd)


    I have this code which shows one stick post for home page:
    <?php
    $sticky = get_option(‘sticky_posts’);
    rsort( $sticky );
    $sticky = array_slice( $sticky, 1);
    query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1 ) );
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class=”sticky-post-block”>
    show sticky post
    </div>
    <?php endwhile; ?>

    <?php } ?>

    <?php
    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $sticky = get_option(‘sticky_posts’);
    $args=array(
    ‘caller_get_posts’=>1,
    ‘post__not_in’=> $sticky,
    ‘paged’=>$paged,
    );
    query_posts($args);
    ?>

    <?php while (have_posts()) : the_post(); ?>

    exclude sticky post, show 4 latest posts

    What I want to achieve here, is if no sticky post selected at all, the first query which pulls the stick posts return nothing.

    Right now, the first query pulls the latest 4 posts and this makes it show a duplication of 4 posts.

    I found this code in Codex but I don’t how to go about “inserting my stuff” to stop sticky post and the duplication from showing in the “sticky-post-block.”

    quote:
    To return just the first sticky post or nothing:

    $sticky = get_option(‘sticky_posts’);
    $args = array(
    ‘posts_per_page’ => 1,
    ‘post__in’ => $sticky,
    ‘caller_get_posts’ => 1
    );
    query_posts($args);
    if($sticky[0]) {
    // insert here your stuff…
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • Just put in a check for is_sticky() like this:

    <?php while (have_posts()) : the_post(); ?>
       <?php if (!is_sticky()) continue; ?>
       <div class="sticky-post-block">
       ...show sticky post ...
       </div>
    <?php endwhile; ?>

    Sweet and simple! Thanks a lot!!

    You are welcome! Now, please use the dropdown at top right to mark this topic ‘Resolved’ so others with the same problem can see that there is a solution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how to: if no sticky post selected, return none’ is closed to new replies.