Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having trouble getting my pagination links to work, or even show up. I've tried several variations of the code, but here is what I have in home.php:

<?php get_header(); ?> 
<?php get_sidebar(); ?> 

<div id="blog">
    <?php if (have_posts()) : ?>  
        <?php while (have_posts()) : the_post(); ?>  
        <div class="post" id="post-<!--?php the_ID(); ?-->">
    <div class="post_meta">
        <h2><a href="<!--?php the_permalink() ?-->" rel="bookmark" title="Permanent Link to 
            <?php the_title(); ?>"><?php the_title(); ?></a></h2>
        <p class="post_date"><?php the_time('F jS, Y') ?></p>
    </div><!-- end blog_meta -->
    <div class="post">
        <?php the_content('Read the rest of this entry »'); ?>
        <img src="<?php bloginfo('template_directory'); ?>/img/dots_small.png" class="divider" alt="post divider">
    </div><!-- end post -->
    <div class="pagination">
        <?php
            previous_post_link('<span class="left">&laquo; %link</span>');
            next_post_link('<span class="right">%link &raquo;</span>');
        ?>                          
    </div><!-- end pagination -->
    <?php endwhile; ?>

     <?php else : ?>  
        <h2 class="center">Not Found</h2>  
        <p class="center">Sorry, but you are looking for something that isn't here.</p>  
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>  
    <?php endif; ?>
</div><!-- end blog -->

 <?php get_footer(); ?> 
share|improve this question

1 Answer 1

up vote 2 down vote accepted

OK, it seems that we have two errors here:

  1. You should use posts_nav_link() instead of previous_post_link() and next_post_link(). The functions you used point to previous/next posts, not pages. You can refer to the WordPress Codex for more information.

  2. You should place posts_nav_link() after endwhile so that it is not repeated for every single post excerpt displayed on the page.

So the code should look like this:

<?php get_header(); ?> 
<?php get_sidebar(); ?> 

<div id="blog">
    <?php if (have_posts()) : ?>  
        <?php while (have_posts()) : the_post(); ?>  
        <div class="post" id="post-<!--?php the_ID(); ?-->">
    <div class="post_meta">
        <h2><a href="<!--?php the_permalink() ?-->" rel="bookmark" title="Permanent Link to 
            <?php the_title(); ?>"><?php the_title(); ?></a></h2>
        <p class="post_date"><?php the_time('F jS, Y') ?></p>
    </div><!-- end blog_meta -->
    <div class="post">
        <?php the_content('Read the rest of this entry »'); ?>
        <img src="<?php bloginfo('template_directory'); ?>/img/dots_small.png" class="divider" alt="post divider">
    </div><!-- end post -->
    <?php endwhile; ?>
    <div class="pagination">
        <?php posts_nav_link(); ?>               
    </div><!-- end pagination -->
     <?php else : ?>  
        <h2 class="center">Not Found</h2>  
        <p class="center">Sorry, but you are looking for something that isn't here.</p>  
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>  
    <?php endif; ?>
</div><!-- end blog -->

 <?php get_footer(); ?> 

Hope that helps, it works for me.

share|improve this answer
    
Thank you for spotting the errors. I've applied your given code to my page, and still the links do not show up. You can see the live site here. –  user1255049 Jun 1 '13 at 16:35
1  
One question then - did you publish more posts than what is set under "Blog pages show at most" label in Admin -> Settings -> Reading? Pagination navigation will appear only if needed, so if your blog is configured to show 10 posts per page and you have only one post, there will be no "next page" link just beacause of that. –  gwdo Jun 1 '13 at 17:27
    
didn't know pagination only appears if needed. After creating a few test posts the pagination works fine. Thanks. –  user1255049 Jun 1 '13 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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