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.

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 shows the loop, but the pagination isn't working. If I click "older posts" the url changes to page-2, but the content is exactly the same.

share|improve this question
    
I think we'll need whole code from you theme's index.php to help you –  Mamaduka Oct 10 '11 at 21:36
    
@Mamaduka This snippet is from a custom post template, I don't see how the index.php will be helpful... –  Jason Oct 10 '11 at 21:50
    
Oh sorry, I thought it was from index.php, than we need code from your theme's custom post template file –  Mamaduka Oct 10 '11 at 22:00
    
@Mamaduka the code is included, the rest is layout. –  Jason Oct 10 '11 at 22:15
add comment

1 Answer 1

This is all sorts of wrong. First don't use start_wp(); I think that was depreciated 4 years ago. Second your loop is messy, query_posts is for altering the main loop, aka not get_posts.

So just write it normally using get_posts or WP Query.

$args = array( 'numberposts' => 1, 
               'offset'=> 0, 
               'category_name' => 'carrs, dominicks, genuardis, etc', 
                );


$the_query = new WP_Query( $args );

while ( $the_query->have_posts() ) : $the_query->the_post();

$count_posts = $the_query->current_post + 1; // use this to count your posts

//your loop stuff
endwhile;

In not sure how you want your pagination to work, if you just want next/previous links use <?php previous_post(); ?> <?php next_post(); ?> for true numbered pagination I recommend a plugin like WP-PageNavi or WP-Paginate that easily integrated into your theme through a function.

http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Next_and_Previous_Links

share|improve this answer
    
see edit. I'm getting the loop, but the pagination isn't working. –  Jason Oct 12 '11 at 22:31
    
Have a look at this , wordpress.stackexchange.com/questions/2638/… –  Wyck Oct 13 '11 at 1:45
    
This doesn't really answer the question of whether you can use pagination on a get_posts array –  AlxVallejo Sep 27 '12 at 20:57
    
His question was altered and the link above answers his question, thus the comment... –  Wyck Sep 28 '12 at 4:13
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.