Maarten Wolfsen Maarten Wolfsen - 3 years ago 179
PHP Question

Loop through wordpress' the_post_navigation() function

Is it possible to loop through the

the_post_navigation()
function?

Now, when I am at the end of the post loop, the 'next post' button simply disappears. I want this button to stay, and to navigate to the first post. This way, the navigation keeps looping through the posts.

This is the code btw:

<div class="case-nav hidden-xs">
<?php the_post_navigation( array(
'prev_text' => __( 'Previous post'),
'next_text' => __( 'Next post'),
)); ?>
</div>


EDIT

Here is the source code:
http://www.codesend.com/view/333b7293fcc8994ae7938056cf8c6b1f/

Answer Source

Okay, I solved it myself, in a different way. But the result is the same!

<!-- Navigation -->
                    <div class="case-nav hidden-xs">
                        <?php the_post_navigation( array(
                        'prev_text' => __( 'Vorig bericht'),
                        'next_text' => __( 'Volgend bericht'),
                        )); ?>
                    </div>
                    <div class="case-nav visible-xs">
                        <?php the_post_navigation( array(
                        'prev_text' => __( 'Vorige'),
                        'next_text' => __( 'Volgende'),
                        )); ?>
                    </div>
                    <?php

                    // Get first post
                    $args = array(
                    'offset'           => 0,
                    'category'         => '',
                    'category_name'    => '',
                    'orderby'          => 'ASC',
                    'order'            => 'DESC',
                    'include'          => '',
                    'exclude'          => '',
                    'meta_key'         => '',
                    'meta_value'       => '',
                    'post_type'        => 'post',
                    'post_mime_type'   => '',
                    'post_parent'      => '',
                    'post_status'      => 'publish',
                    'suppress_filters' => true
                    );
                    $posts_array = get_posts( $args );
                    $firstPost = get_permalink($posts_array[0]->ID);

                    //Get latest post
                    wp_list_pages('title_li=&depth=1');
                    query_posts('posts_per_page=1');
                    if(have_posts());
                    while(have_posts()) :
                    the_post();
                        $lastPost = get_permalink();
                    endwhile;
                    wp_reset_query();
                    ?>

                    <script type="text/javascript">
                    var itemNext = "<?php echo $firstPost; ?>";
                    var itemPrev = "<?php echo $lastPost; ?>";
                    //      remove nav line if nav item doesn't exist
                                if(!$('.nav-previous').length){
                                    $('.nav-links').append('<div class="nav-previous"><a href="' + itemNext + '" rel="prev">Vorig bericht</a></div>');
                                } else if(!$('.nav-next').length){
                                    $('.nav-links').append('<div class="nav-next"><a href="' + itemPrev + '" rel="next">Volgend bericht</a></div>');
                                } else{}
                    //      -- remove nav line if nav item doesn't exist
                    </script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download