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.

This code works for page 1

<?php
            $paged1 = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $recent1 = get_transient( 'recent1' );
                                if ( false === $recent1) {
    $recent1 = new WP_Query ('cat=3&posts_per_page=5'.'&paged='.$paged1);
    set_transient('recent1', $recent1, 60*60);
    }
    // do normal loop stuff
    if ($recent1->have_posts()) : while ($recent1->have_posts()) : $recent1->the_post();
    ?>
    <div id="line"><a href="<?php the_permalink () ?>" rel="bookmark">
 <h2><?php the_title (); ?></h2>
 <?php the_post_thumbnail(array(100,100), array('class' => 'alignleft')); ?>
 <?php the_advanced_excerpt(); ?>

But there is a problem with pagination. Page 1 confused with page 2 or page 3 and etc. On page/2 there is content of page/1 or on the contrary. How to resolve this ?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

"This code works" - but it doesn't!

If you're gonna cache paginated posts, you'll need to store them chunked:

if ( ! $my_paged = absint( get_query_var( 'paged' ) ) )
    $my_paged = 1;

if ( ! $my_query = get_transient( "recent_$paged" ) ) {
    $my_query = new WP_Query( "cat=3&posts_per_page=5&paged=$my_paged" );
    set_transient( "recent_$paged", $my_query, 60 * 60 );
}
share|improve this answer
    
thank you very much. Is it right ?<?php if ( ! $my_paged = absint( get_query_var( 'paged' ) ) ) $my_paged = 1; if ( ! $my_query = get_transient( "recent_$paged" ) ) { $my_query = new WP_Query( "cat=3&posts_per_page=5&paged=$my_paged" ); set_transient( "recent_$paged", $my_query, 60 * 60 ); } while ($my_query->have_posts()) : $my_query->the_post(); ?> <h2><?php the_title (); ?></h2> <?php endwhile; ?><?php wp_reset_query(); ?> –  Ronin Jul 24 '12 at 10:41
    
Why, does it not work? –  TheDeadMedic Jul 24 '12 at 11:08

There is a problem with pagination because you do not take pagination into consideration while storing and retrieving your transient. Give this a try:

// Find out which page this is. If 'page' is not a number, set page to 1.
$page = ( ctype_digit( $_REQUEST['page'] ) ) ? $_REQUEST['page'] : 1;

$paged1 = (get_query_var('paged')) ? get_query_var('paged') : 1;
$recent1 = get_transient( 'recent'.$page );

if ( false === $recent1) {
    $recent1 = new WP_Query ('cat=3&posts_per_page=5'.'&paged='.$paged1);
    set_transient('recent'.$page, $recent1, 60*60);
}

Also, if you're not aware of it already, look up on "WP Super Cache".

Edit: TheDeadMedic beat me to it.

share|improve this answer

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.