循环通过wordpress'the_post_navigation()函数

时间:2016-04-19 12:23:30

标签: php wordpress loops

是否可以循环使用the_post_navigation()函数?

现在,当我在帖子循环结束时,“下一个帖子”按钮就会消失。我希望这个按钮保持不变,并导航到第一篇文章。这样,导航就可以循环播放帖子。

这是代码btw:

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

修改

这是源代码: http://www.codesend.com/view/333b7293fcc8994ae7938056cf8c6b1f/

2 个答案:

答案 0 :(得分:1)

好吧,我以不同的方式自己解决了这个问题。但结果是一样的!

<!-- 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>

答案 1 :(得分:1)

这是帖子之间无限循环的较短版本。

/* Infinite next and previous post looping */
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();
};