如何将next_post_link添加到以下循环中

时间:2018-01-30 07:26:30

标签: php wordpress pagination

我尝试添加df1 <- structure(list(years = c(2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L ), topics = c("V1", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V2", "V20", "V21", "V22", "V23", "V24", "V25", "V26", "V27", "V28", "V29", "V3", "V30", "V4", "V5", "V6", "V7", "V8", "V9", "V1", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V2", "V20", "V21", "V22", "V23", "V24", "V25", "V26", "V27", "V28", "V29", "V3", "V30", "V4", "V5", "V6", "V7", "V8", "V9"), probability = c(0.045, 0.0553, 0.03038, 0.0454189, 0.0347, 0.0278, 0.0164, 0.030016, 0.0205, 0.0212, 0.0434, 0.0506, 0.0376, 0.019, 0.04, 0.033, 0.019, 0.0499, 0.0204, 0.049, 0.02044, 0.03, 0.0207, 0.0219, 0.035, 0.019, 0.044, 0.037, 0.0327, 0.046, 0.021, 0.03015, 0.028, 0.0299, 0.015, 0.0439, 0.0378, 0.013, 0.0241, 0.0454, 0.0226, 0.0207, 0.0258, 0.0237, 0.063, 0.027, 0.018, 0.058, 0.0255, 0.0172, 0.0576, 0.0706, 0.035, 0.0714, 0.0266, 0.0228, 0.0183, 0.0265, 0.0376, 0.0409)), .Names = c("years", "topics", "probability"), class = "data.frame", row.names = c(NA, -60L)) ,但它不适用于此循环。我该如何解决这个案子?我想显示10个帖子,并阅读下一个10个帖子的更多链接等等。谢谢。

<?php next_posts_link('Read more »', 10); ?>

2 个答案:

答案 0 :(得分:1)

尝试使用分页。

<?php
$paged = ( isset( $_GET['pg'] ) && intval( $_GET['pg'] ) > 0 )? intval( $_GET['pg'] ) : 1;
query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 10 ) );

?>
<?php if ( have_posts() ) : ?>
    <?php while( have_posts() ) : the_post(); ?>
        <div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="post-excerpt">
                <?php the_excerpt(); ?>
            </div>
        </div>
    <?php endwhile; ?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
        <div class="pagination">
            <?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) { 
                $link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
                echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
            } ?>
        </div>
    <?php endif ?>
<?php else : ?>
    <div class="404 not-found">
        <h3>Not Found</h3>
        <div class="post-excerpt">
            <p>Sorry, but there are no more posts here... Please try going back to the <a href="<?php echo remove_query_arg( 'pg' ); ?>">main page</a></p>
        </div>
    </div>
<?php endif;

// Make sure the default query stays intact
wp_reset_query();

答案 1 :(得分:1)

您需要使用paged变量来告知您在分页列表中的位置。

在循环前添加此行:$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';然后将其添加到您的查询参数ar 'nopaging' => false, 'paged' => $paged,

然后,您可以使用previous_posts_link( '« Newer Entries' );next_posts_link( 'Older Entries »', $query->max_num_pages );分别在while/endwhile循环之前和之后获取上一个/下一个链接。

顺便说一下,理想情况下,while循环应该包含在if ( $loop->have_posts() )语句中。

希望有所帮助