0

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 ?

3

"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 );
}
  • 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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.