Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pagination WordPress

  • SOLVED

I am having some difficulty getting pagination to work.

The pagination links show up but when go to /page/2/ all of page 1's content still displays and the links still state that I am on page one still.

Here is the code im using


<?php //The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => 'press',
‘paged’=>$paged,
'order' => 'DESC',
‘posts_per_page’=>'12'
);
query_posts($args); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<!-- ALL MY CONTENT HERE -->

<?php endwhile; // end of the loop. ?>

<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => False,
) );
?>

<?php get_footer() ?>


Any Ideas why it's doing that?

Answers (3)

2012-12-13

Dbranes answers:

Hi, the functions <strong>previous_posts_link()</strong> and <strong>next_posts_link()</strong> are using the global <strong>$wp_query</strong> object.

The definition in the core is fx:

/**
* Return the next posts page link.
*
* @since 2.7.0
*
* @param string $label Content for link text.
* @param int $max_page Optional. Max pages.
* @return string|null
*/
function get_next_posts_link( $label = null, $max_page = 0 ) {
global $paged, $wp_query;

if ( !$max_page )
$max_page = $wp_query->max_num_pages;



So you can try to use the following page template, where you "hijack" the global $wp_query object for your custom loop, but we just have to remember to reset it in the end :

<?php
/*
Template Name: Press
*/
get_header(); ?>

<div id="content">

<h1 class="page-title">
<?php the_title(); ?>
</h1>

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php if ( have_posts() ) :

$temp = $wp_query;
$wp_query = null;

$my_query_args=array(
'post_type'=>'press',
'posts_per_page' => 12,
'offset' => 0,
'orderby'=>'date',
'order'=>'DESC',
'paged'=>$paged,
);

$wp_query = new WP_Query($my_query_args);
?>

<div class="prevnext">
<p>
<span class="left">
<?php echo 'Page '. $paged . ' of ' . $wp_query->max_num_pages; ?>
</span>
<?php previous_posts_link('Previous'); ?>
<?php next_posts_link('Next'); ?>
</p>
</div>

<?php
while ($wp_query->have_posts()) : $wp_query->the_post();?>
<div id="post-<?php the_ID(); ?>" <?php post_class();?>>
<h2 class="entry-title"><?php the_title();?></h2>
<p><?php the_content();?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>


<div class="pagination">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('&laquo; Previous'),
'next_text' => __('Next &raquo;')
));
?>
</div>

<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>

</div><!-- end #content-->

<?php get_footer(); ?>

2012-12-13

John Cotton answers:

There doesn't immediately appear to be any wrong with your code.

Is this on standard page or a custom post type? Or Somewhere else?

One common reason for paging not working when on pages on than the normal is missing/incorrect regex expressions in the rewrite rules.


floy comments:

Standard Page template displaying a custom post type


John Cotton comments:

global $wp_rewrite; print_r($wp_rewrite->rules);

Have a look at the rewrite rules and check that there is one with the correct structure to match the url you have.


John Cotton comments:

And check that the rule is setting the <em>paged</em> queryvar and not the <em>page</em> one.

2012-12-13

Arnav Joy answers:

try this

<?php //The Query

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

$args=array(

'post_type' => 'press',

'paged'=>$paged,

'order' => 'DESC',

'posts_per_page'=>'12'

);

//query_posts($args);

$query = new WP_Query($args);

?>



<?php if ( $query->have_posts() ) while ( $query->have_posts() ) : $query->the_post(); ?>



<!-- ALL MY CONTENT HERE -->



<?php endwhile; // end of the loop. ?>



<?php

global $wp_query;



$big = 999999999; // need an unlikely integer



echo paginate_links( array(

'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

'format' => '?paged=%#%',

'current' => max( 1, get_query_var('paged') ),

'total' => $wp_query->max_num_pages,

'prev_next' => False,

) );

?>



<?php get_footer() ?>


floy comments:

The paging system now works if i put it in manually eg; URLHERE/page/2 into the url bar.

The pagination down the bottom shows nothing.

I also added in the next_posts_link and previous_posts_link to the mix to see if they showed up. On page one it doesnt but on page 2 (entered manually) it shows the NEWER link.


floy comments:

The paging system now works if i put it in manually eg; URLHERE/page/2 into the url bar.

The pagination down the bottom shows nothing.

I also added in the next_posts_link and previous_posts_link to the mix to see if they showed up. On page one it doesnt but on page 2 (entered manually) it shows the NEWER link.


Arnav Joy comments:

try this

<?php //The Query



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



$args=array(



'post_type' => 'press',



'paged'=>$paged,



'order' => 'DESC',



'posts_per_page'=>'12'



);



//query_posts($args);



$query = new WP_Query($args);



?>







<?php if ( $query->have_posts() ) while ( $query->have_posts() ) : $query->the_post(); ?>







<!-- ALL MY CONTENT HERE -->







<?php endwhile; // end of the loop. ?>







<?php



global $wp_query;







$big = 999999999; // need an unlikely integer







echo paginate_links( array(



'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),



'format' => '?paged=%#%',



'current' => max( 1, get_query_var('paged') ),



'total' => $query->max_num_pages,



'prev_next' => False,



) );



?>







<?php get_footer() ?>