基本WordPress循环 - 分页


Basic WordPress Loop - Pagination?

那么我错过了什么吗? 因为如果我执行以下操作 - 在数据库包含 4 个帖子时single.php

if(have_posts()){
    while(have_posts()){
        the_post();
        // post content here
    }
    var_dump(get_next_posts_link('asdasdsad')); die();
}

我得到空。这似乎真的很不对劲。这只是你基本的旧循环.....数据库中还有其他 4 个帖子...那么我失败了吗?我知道这个函数必须在循环中使用。

帮助?

你的函数不会在循环中被调用。

试试这个:

   <?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// get_next_posts_link() usage with max_num_pages
echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer Entries' );
?>
<?php 
// clean up after our query
wp_reset_postdata(); 
?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

正确的方法是按照我所做的去做,但在 if 语句中,我需要分别使用 next_post_linkprevious_post_link。 因为这些让我在下一篇文章和上一篇文章之间移动。