• 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?

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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,
            ));
    ?>
    Thread Starter daricedotorg

    (@daricedotorg)

    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>

    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(); ?>

    Thread Starter daricedotorg

    (@daricedotorg)

    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?

    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>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Pagination on single.php’ is closed to new replies.