Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

previous_posts_link returns an empty query WordPress

Under >Settings >Reading I have my Front page display set to a static page.

My Front page is set to a static page I created called 'Home Page' which loads my front-page.php template.

My Posts page is set to a page I created called 'Blog' and it pulls in my index.php template.

The problem I'm having is with the Blog page. I've set the query to only show 1 post per page so that I can test the next and previous post links.

If you click on the 'Previous Posts' link it returns an empty query when it should be loading the previous post. Not sure why this would be happening cause the are 3 posts in total which are all assigned to different categories.

Here's the link to the page:

Here's my code:

<?php get_header(); ?>

<div id="main-content-wrap" class="group">

<section id="main-content" class="page group floatleft">

<?php get_sidebar('blog'); ?>

<section id="content" class="posts group floatleft">

<?php if (have_posts()) : ?>

<?php if (is_category()) { ?>

<h2>Category: <?php single_cat_title(); ?></h2>

<?php } elseif (! is_category()) { ?>

<h2>Recent Posts</h2>

<?php } ?>

<?php global $wp_query; ?>
<?php $args = array_merge( $wp_query->query, array( 'posts_per_page' => '1' ) ); ?>
<?php query_posts($args); ?>
<?php while (have_posts()) : the_post(); ?>

<article class="group">

<h3 class="post-hd">
<a class="more-link" title="Link to <?php the_title(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
<div class="meta textright">
<span class="post-author">written by <?php the_author_meta('first_name'); ?></span>
<span class="meta-sep">|</span>
<span class="post-date"><?php the_time('F jS, Y') ?></span>
</div><!-- meta -->

<?php if ( has_post_thumbnail() ) { ?>
<div class="post-thumb group">
<?php the_post_thumbnail('large'); ?>
</div><!-- thumb -->
<?php } ?>

<div class="entry group">
<?php the_excerpt(); ?>
<a class="more-link" title="Link to <?php the_title(); ?>" href="<?php the_permalink(); ?>">
read full article &rsaquo;
</div><!-- entry -->

</article><!-- post-ID -->

<?php endwhile; ?>

<nav class="posts-nav">
<div class="prev-posts btn"><?php next_posts_link('Older Entries') ?></div>
<div class="next-posts btn"><?php previous_posts_link('Newer Newer') ?></div>
</nav><!-- posts-nav -->

<?php else : ?>

<article id="not-found" class="group">

<h2>Nothing Found</h2>
<p>There are currently no posts assigned to this page.</p>

</article><!-- not-found -->

<?php endif; ?>

</section><!-- content -->

</section><!-- main-content -->

<?php get_sidebar('right'); ?>

</div><!-- main-content-wrap -->

<?php get_footer(); ?>


Answers (1)


Dylan Kuhn answers:

The message "There are currently no posts assigned to this category." makes me think that [[LINK href=""]]page 2[[/LINK]] is not loading the index.php template. To me that indicates an issue with permalinks or canonical redirect. Those can be hard to troubleshoot, but re-saving your permalink settings is always worth a try.

Denis Leblanc comments:


It is loading the index.php template. The text that appears is assigned in the else statement near the bottom of the template. Have a look at the code again and you'll see it there near the bottom.