WordPress pagination showing same posts on each page

I need to make adjustments to a horribly written WP theme that (a custom theme that was written in tables, and bad code).

The theme has several custom templates, but pagination wasn’t used and get_posts was used in place of query_posts –

Read More
    <?php query_posts('showposts=1'); ?>
    <?php $posts = get_posts('numberposts=10&offset=0&category_name=albertsons, carrs, dominicks, genuardis, heb, kroger, pavillions, publix, randalls,safeway,shop-rite,tom-thumb,vons,whole-foods'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "10") { break; } else { ?>

...

    <?php $count2++; } ?>
    <?php endforeach; ?>

I need to get pagination to work with get_posts, or rewrite the function to use query_posts only, so that I can add 'paged' => get_query_var('page')

When I try to rewrite to only use query_posts, the whole damn thing breaks.

Any thoughts on how to improve?

Thanks

UPDATE:

<?php 
global $wp_query;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;


query_posts(array('posts_per_page' => '3','paged'=>$paged,'category_name'=>'albertsons, carrs, dominicks, genuardis, heb, kroger, pavillions, publix, randalls,safeway,shop-rite,tom-thumb,vons,whole-foods')); ?>
                    ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Which works, but is not paginating. “Older posts” will reload the page with the url page-2, but the content is exactly the same – meaning the exact same posts are showing as on first page.

Solved – needed to use

Related posts

Leave a Reply

1 comment

  1. You can replace the code with something like this

    <?php
    global $wp_query;
    $limit = get_option('posts_per_page');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    
    query_posts(array('posts_per_page'=>$limit,'paged'=>$paged,'category_name'=>'albertsons, carrs, dominicks, genuardis, heb, kroger, pavillions, publix, randalls,safeway,shop-rite,tom-thumb,vons,whole-foods'));
    
    
    /* you may want to uncomment the below two lines if you are using custom page template*/
    //$wp_query->is_archive = true; 
    //$wp_query->is_home = false;
    

    then call normal Post Loop like

    if(have_posts()):
      while(have_posts()):the_post();
       the_content() ;//or so on
      endwhile;
    endif; 
    

    Btw, I will advice going against query_posts/get_posts and use WP_Query

    Hope that helps to get you started 🙂