Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On my Blog http://flattrendz.com/, I currently want to display the latest sticky post.

I used the code of http://justintadlock.com/archives/2009/03/28/get-the-latest-sticky-posts-in-wordpress

    <?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 2 newest stickies (change 2 for a different number) */
$sticky = array_slice( $sticky, 0, 2 );

/* Query sticky posts */
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>

<!-- BLOG BAR -->
<div class="blog_bar <?php echo $blog_bar_class; ?>">
    <div class="container">

        <div class="text-center">
            <h2 class="lead">

                <span><?php printf(__('From Our Blog', 'dot')); ?> </span>
                <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('%s', 'dot'), get_the_title()); ?>"><?php the_title(); ?> <i class="icon-long-arrow-right"></i></a>
            </h2>
        </div>

    </div>
</div>
<!-- // BLOG BAR -->

<?php endwhile;?>
<?php
  }
    else { }

 /* Restore original Post Data */
 wp_reset_query();
?>

What I need to do is if there are posts marked as sticky post, then display just the latest sticky post, or else simply display the latest post.

share|improve this question

1 Answer 1

I realized the issue. I missed the posts_per_page 'posts_per_page' => 1. Now it works.

share|improve this answer
    
Can you explain why that solved the problem? –  s_ha_dum Jul 17 '13 at 1:05
    
Hi, my assumption is that since the number of entries to return was mentioned in the sticky post array $sticky = array_slice( $sticky, 0, 2 ); and when there were no sticky posts, the number of regular posts displayed was based on the number of results I set to show per page from the settings in the backend. Adding posts_per_page fixed this by returning only 1 entry. –  Harish Chouhan Jul 17 '13 at 14:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.