Next and Previous loop
Question
I am making a wordpress site but i need little help,
What I have is a function with Next and Previous buttons to list projects, but when i go to the last project the Next button is gone.
What I want to do is, when I go to the last project I hit "Next" and direct me to the first project something like loop slide
Here is my code:
<?php if (has_term( 'type-1', 'projecttype' ) ) { ?>
<div class="holder">
<?php if ( have_posts() ) : ?>
<div class="container-fluid">
<div class="project-paginat">
<?php if(get_post_meta($post->ID, "_type", true)){ ?>
<ul class="list-inline">
<li><a href="<?php echo get_home_url(); ?>/our-work">Back to Our Work</a></li>
<li>>> <?php echo get_post_meta($post->ID, "_type", true); ?></li>
<li><?php previous_post_link('%link', '< Previous Project', true, '', 'projectcategory') ?></li>
<li><?php next_post_link('%link', 'Next Project >', true, '', 'projectcategory') ?></li>
</ul>
<?php } else {
echo '<p>There Is No Project Type To Display.</p>';
} ?>
</div>
Solution
Use get_adjacent_post to first see if there's actually a next post:
if (get_adjacent_post (false, '', false)) {
next_post_link ('%link', 'Next Project >', true, '', 'projectcategory');
} else {
// manually create link to first post here
}
This site shows one way of actually getting the first post's link, such as with a WP_Query call and then calling get_permalink. The exact code depends on the types of post you are showing.
OTHER TIPS
Using the link in iguanarama's answer, your updated code would be:
<?php if (has_term( 'type-1', 'projecttype' ) ) { ?>
<div class="holder">
<?php if ( have_posts() ) : ?>
<div class="container-fluid">
<div class="project-paginat">
<?php if(get_post_meta($post->ID, "_type", true)){ ?>
<ul class="list-inline">
<li><a href="<?php echo get_home_url(); ?>/our-work">Back to Our Work</a></li>
<li>>> <?php echo get_post_meta($post->ID, "_type", true); ?></li>
<li>
<?php
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '< Previous Project', true, '', 'projectcategory');
} else {
$args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'category_name' => 'projectcategory',
);
$first = new WP_Query( $args ); $first->the_post();
echo '<a href="' . get_permalink() . '">< Previous Project</a>';
wp_reset_query();
};
?>
</li>
<li>
<?php
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Project >', true, '', 'projectcategory');
} else {
$args = array(
'posts_per_page' => 1,
'order' => 'ASC',
'category_name' => 'projectcategory',
);
$last = new WP_Query( $args ); $last->the_post();
echo '<a href="' . get_permalink() . '">Next Project ></a>';
wp_reset_query();
};
?>
</li>
</ul>
<?php } else {
echo '<p>There Is No Project Type To Display.</p>';
} ?>
</div>
Reference: http://wplancer.com/infinite-next-and-previous-post-looping-in-wordpress/
You can use get_adjacent_post, see below working example.
<?php
/**
* Infinite next and previous post looping in WordPress
*/
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← Previous Post');
} else {
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
echo '<a href="' . get_permalink() . '">← Previous Post</a>';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post →');
} else {
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
echo '<a href="' . get_permalink() . '">Next Post →</a>';
wp_reset_query();
};
?>