Next and Previous loop

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"
                lia href="?php echo get_home_url(); ?/our-work"Back to Our Work/a/li
                li nbsp;?php echo get_post_meta($post-ID, "_type", true); ?/li
                li?php previous_post_link('%link', 'lt; Previous Project', true, '', 'projectcategory') ?/li 
                li?php next_post_link('%link', 'Next Project gt;', true, '', 'projectcategory') ?/li     
            /ul 
            ?php } else {
                echo 'pThere Is No Project Type To Display./p';
            } ?
        /div

Topic next previous loop post-meta Wordpress

Category Web


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', '&larr; Previous Post');
        } else {
            $first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
                echo '<a href="' . get_permalink() . '">&larr; Previous Post</a>';
            wp_reset_query();
        };

        if( get_adjacent_post(false, '', false) ) {
            next_post_link('%link', 'Next Post &rarr;');
        } else {
            $last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
                echo '<a href="' . get_permalink() . '">Next Post &rarr;</a>';
            wp_reset_query();
        };

    ?>

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>>> &nbsp;<?php echo get_post_meta($post->ID, "_type", true); ?></li>
                    <li>
                    <?php
                        if( get_adjacent_post(false, '', true) ) {
                            previous_post_link('%link', '&lt; 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() . '">&lt; Previous Project</a>';
                            wp_reset_query();
                        };

                    ?>
                    </li>
                    <li>
                    <?php
                        if( get_adjacent_post(false, '', false) ) {
                            next_post_link('%link', 'Next Project &gt;', 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 &gt;</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/


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 &gt;', 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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.