Last active
August 29, 2015 14:25
-
-
Save ismaeltoe/2a0f68046bd53c3c5110 to your computer and use it in GitHub Desktop.
[Wordpress] Display just the newest sticky post, if none return nothing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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