WordPress.org

Ready to get started?Download WordPress

Forums

Pagination on single.php (6 posts)

  1. daricedotorg
    Member
    Posted 3 years ago #

    I'm trying to implement pagination on a website I did not code(thus the old WP version).

    The following code lists 5 posts like asked but when clicking the navigation it will just change the URL to http://mysite.com?p=1516&paged=2 the content won't change to list the next 5 posts.

    Code in single.php

    <?php
           query_posts('posts_per_page=5');
           if (have_posts()) : while (have_posts()) : the_post();
                 ?>
    
     <li><a>"><?php the_title(); ?></a> <div class="date">{<?php the_time('l, j F, Y') ?>}</div></li>
    
     <?php endwhile; 
    
       previous_posts_link(); ?> • <?php next_posts_link(); 
    
    endif; ?>

    Can anyone point out what is wrong with the code and how to solve the issue?

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    you have to paginate a query:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array(
    	'posts_per_page' => 5,
             'paged'=>$paged,
            ));
    ?>
  3. daricedotorg
    Member
    Posted 3 years ago #

    I tried the following but still the same issue:

    <?php
      if (have_posts()) :
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array('posts_per_page' => 5,'paged'=>$paged,));
    
       while (have_posts()) : the_post();  ?>
    
     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <div class="date">{<?php the_time('l, j F, Y') ?>}</div></li>
    
    <?php endwhile; 
    
    previous_posts_link(); ?> &bull; <?php next_posts_link();
               endif; ?>
     		</ul>
  4. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    you've got the query inside the loop, it comes first, before the 'if'

    for instance mine looks like:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array(
    	'post_type' => 've_products', // custom post type
             'paged'=>$paged,
            ));
    ?>
             <?php if (have_posts()) : ?>
             <?php while (have_posts()) : the_post(); ?>
  5. daricedotorg
    Member
    Posted 3 years ago #

    Weird...still same issue. Do I have to modify anything else in the settings and is the use of previous_posts_link(); ?> &bull; <?php next_posts_link(); correct?

  6. mjoanisse
    Member
    Posted 3 years ago #

    there is a typo in the query defined in your above statement -
    'paged'=>$paged,));

    try this -

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    
    $args = array (
      'posts_per_page' => 5,
      'paged' => $paged
    );
    
    query_posts($args);
    if (have_posts()) : while (have_posts()) : the_post();
    ?>
    
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <div class="date">{<?php the_time('l, j F, Y') ?>}</div>
    </li>
    
    <?php endwhile; ?>
    <?php previous_posts_link('< previous page', 0); ?>
    <?php next_posts_link('next page >', 0); ?>
    <?php else: endif; ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic