问题
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/
回答1:
This is a shorter version for infinite looping between posts.
/* Infinite next and previous post looping */
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();
};
回答2:
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>
来源:https://stackoverflow.com/questions/36718287/loop-through-wordpress-the-post-navigation-function