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

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

share|improve this question
add comment

migrated from stackoverflow.com Oct 11 '11 at 10:43

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 2 down vote accepted

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 :)

share|improve this answer
    
well, the query seems to work but the paging doesn't. The page, if you click "older posts" shows page-2 in the url, but the content is the same. –  Jason Oct 12 '11 at 22:08
add comment

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.