Learning how to build WordPress themes from scratch I have run into an issue when I try to code my home.php. The issue I run into is with the pagination. After the last page of posts instead of stopping/disabling the button it still enables the button and once clicked it loaded /page/# using the index.php file which at this time is just the <?php get_header(); ?> and <?php get_footer(); ?>. The functions I have tested are with <?php next_posts_link(); ?> and <?php echo get_next_posts_link(); ?>. I have researched awhile on this and ran across it may have something to do with the permalinks and they are set to post name.

Code in home.php:

if ( have_posts() ): while ( have_posts() ): the_post(); ?>
    <div class="thepost">
      <div class="primecircle">
        <div class="circledate">
          <div class="blogmonth"><?php the_time('M'); ?></div>
          <div class="blogdate"><?php the_time('j'); ?></div>
          <div class="blogyear"><?php the_time('Y'); ?></div>
    <div class="blogcontent">
      <h1 class="blogtitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
      <p><?php the_excerpt(); ?></p>
      <div class="learnmore"><a href="<?php the_permalink(); ?>">learn more</a></div>
<?php endwhile; ?>

<div class="pagelinks">
  <ul class="tags" class="list-inline">
    <li><?php echo previous_posts_link('Back'); ?></li>
    <li><?php echo next_posts_link('Next', 5); ?></li>

<?php else: ?>
    <h1 class="blogtitle">No Current blog posts at this time.</h1>      
<?php endif; ?> 

Any suggestions or pointers on how I can properly disable next_posts_link() function when it reaches the last page of posts?

Edited: Adjusted error on the function previous_posts_link(); when I was passing it an label and max_page when it only takes a label. After removing max_page from both functions it stopped cycling to index.php.


Your problem might be related to 5. The previous_posts_link only accepts one valid parameter, and that is $label, you are passing two.

Secondly, the next_posts_link does accept the second parameter $max_pages. Here you ara passing a value of 5, which overrides $wp_query->max_num_pages. So you are telling Wordpress there are 5 pages, regardless of the real amount of pages.


Simply remove 5 from your links, that should solve your problem

  • Is this the only code that you have on your page. Are you running any custom query on it for a slider or featured post. Why do you have index.php and home.php – Pieter Goosen Sep 16 '14 at 5:40
  • Ah that worked. I was building the archive.php and realized I still needed to modify home.php. +1 for that. – DᴀʀᴛʜVᴀᴅᴇʀ Sep 16 '14 at 5:43
  • Great, glad you solved your problem. Enjoy :-) – Pieter Goosen Sep 16 '14 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.