简体   繁体   English

循环遍历wordpress'the_post_navigation() function

[英]Loop through wordpress' the_post_navigation() function

Is it possible to loop through the the_post_navigation() function?是否可以循环通过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/这里是源代码: http://www.codesend.com/view/333b7293fcc8994ae7938056cf8c6b1f/

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>

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', '&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();
}; 

@Hilario Goes method is the best, but to make it work in Custom Post types we need to pass the post_type value. @Hilario Goes 方法是最好的,但要使其在自定义帖子类型中工作,我们需要传递 post_type 值。 Use the following code:使用以下代码:

<?php

/* Infinite next and previous post looping */
if( get_adjacent_post(false, '', true) ) { 
    previous_post_link('%link', '&larr; Previous Post');
} else { 
    $arg_prev = array(
        'post_type' => 'mycptname',
        'posts_per_page' => 1,
        'order'=>'DESC'
    );
    $first = new WP_Query($arg_prev); 
    $first->the_post();
    echo '<a href="' . get_permalink() . '" class="prev-link"> &larr; Previous Post</a>';
    wp_reset_query();
}; 

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM