Skip to content

Instantly share code, notes, and snippets.

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 accrane/8728f15a4c8cdf734c4aa3a35e6aa960 to your computer and use it in GitHub Desktop.
Save accrane/8728f15a4c8cdf734c4aa3a35e6aa960 to your computer and use it in GitHub Desktop.
Infinite next and previous post looping but in same term in WordPress
<?php
/**
* Infinite next and previous post looping in WordPress
*
* This will stay in same Custom Taxonomy Term. Also NEXT and PREVIOUS
* are switched to give the impression of "Going Forard and Back"
*/
if( get_adjacent_post(true, '', false, 'CUSTOM_TAX') ) {
next_post_link( '%link', 'PREVIOUS', TRUE, ' ', 'CUSTOM_TAX' );
} else {
$args = array(
'post_type'=>'CUSTOM_POST_TYPE',
'posts_per_page' => 1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'CUSTOM_TAX', // your custom taxonomy
'field' => 'slug',
'terms' => array( 'term' ) // the terms (categories) you created
)
)
);
$first = new WP_Query($args);
$first->the_post();
echo '<a href="' . get_permalink() . '">PREVIOUS</a>';
wp_reset_query();
};
if( get_adjacent_post(true, '', true, 'CUSTOM_TAX') ) {
previous_post_link( '%link', 'NEXT', TRUE, ' ', 'CUSTOM_TAX' );
} else {
$args = array(
'post_type'=>'CUSTOM_POST_TYPE',
'posts_per_page' => 1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'CUSTOM_TAX', // your custom taxonomy
'field' => 'slug',
'terms' => array( 'term' ) // the terms (categories) you created
)
)
);
$first = new WP_Query($args);
$first->the_post();
echo '<a href="' . get_permalink() . '">NEXT</a>';
wp_reset_query();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment