Skip to content

Instantly share code, notes, and snippets.

@gagimilicevic
Forked from gbyat/latest-and-first.php
Created September 4, 2017 09:20
Show Gist options
  • Save gagimilicevic/2289ba7d690b58e3deba381f4fc94db0 to your computer and use it in GitHub Desktop.
Save gagimilicevic/2289ba7d690b58e3deba381f4fc94db0 to your computer and use it in GitHub Desktop.
get latest and first post in WordPress
/******************************************
* get latest post
* use in loop if ( is_latest() ) { stuff; }
******************************************/
function is_latest() {
global $post;
$loop = get_posts( 'numberposts=1' );
$latest = $loop[0]->ID;
return ( $post->ID == $latest ) ? true : false;
}
/******************************************
* get first post
* use in loop if ( is_first() ) { stuff; }
******************************************/
function is_first() {
global $post;
$loop = get_posts( 'numberposts=1&order=ASC' );
$first = $loop[0]->ID;
return ( $post->ID == $first ) ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment