Skip to content

Instantly share code, notes, and snippets.

@ismaeltoe
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismaeltoe/2a0f68046bd53c3c5110 to your computer and use it in GitHub Desktop.
Save ismaeltoe/2a0f68046bd53c3c5110 to your computer and use it in GitHub Desktop.
[Wordpress] Display just the newest sticky post, if none return nothing
<?php
/* Get all Sticky Posts */
$sticky = get_option( 'sticky_posts' );
?>
<?php if ( !empty($sticky) ) : ?>
<?php
/* Sort Sticky Posts, newest at the top */
rsort( $sticky );
/* Get top 1 Sticky Post */
$sticky = array_slice( $sticky, 0, 1 );
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
/* Query Sticky Posts */
$query = new WP_Query( $args );
?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $classes = get_post_class(); ?>
<div id="post-<?php echo get_the_id(); ?>" class="<?php echo implode(' ', $classes); ?>">
<?php echo get_the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment