Skip to content

Instantly share code, notes, and snippets.

@a1iraxa
Forked from gbyat/latest-and-first.php
Created May 23, 2019 10:36
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 a1iraxa/7e26b539be1613326036237ddb9b4e35 to your computer and use it in GitHub Desktop.
Save a1iraxa/7e26b539be1613326036237ddb9b4e35 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