Last active
August 25, 2021 10:30
-
-
Save treetrum/3a843c961aed69c732f0a61da854b16d to your computer and use it in GitHub Desktop.
Get Next or Previous Post (Looped). Will continue working once end of posts reached.
This file contains hidden or 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 | |
/** | |
* Gets the next post in the current post type, even if is last post | |
* Inspired from https://gist.github.com/banago/5603826 | |
* | |
* @method get_next_post_looped | |
* @return WordPress Post Object | |
*/ | |
function get_next_post_looped() { | |
$post_type = get_post_type(); | |
if ( get_adjacent_post( false, '', false) ) { | |
return get_next_post(); | |
} else { | |
$loop = new WP_Query( 'posts_per_page=1&order=ASC&post_type=' . $post_type ); | |
$loop->the_post(); | |
$postToReturn = get_post(); | |
wp_reset_query(); | |
return $postToReturn; | |
} | |
} | |
/** | |
* Gets the previous post in the current post type, even if is last post | |
* Inspired from https://gist.github.com/banago/5603826 | |
* | |
* @method get_previous_post_looped | |
* @return WordPress Post Object | |
*/ | |
function get_previous_post_looped() { | |
$post_type = get_post_type(); | |
if ( get_adjacent_post( false, '', true) ) { | |
return get_previous_post(); | |
} else { | |
$loop = new WP_Query( 'posts_per_page=1&order=DESC&post_type=' . $post_type ); | |
$loop->the_post(); | |
$postToReturn = get_post(); | |
wp_reset_query(); | |
return $postToReturn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment