WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to: if no sticky post selected, return none (4 posts)

  1. alsd
    Member
    Posted 3 years ago #

    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...
    }

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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; ?>
  3. alsd
    Member
    Posted 3 years ago #

    Sweet and simple! Thanks a lot!!

  4. vtxyzzy
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.