Ask your WordPress questions! Pay money and get answers fast! (more info)

Pagination Not Working on Front Page

  • SOLVED

Hello!
I have a small annoying issue here with pagination. I think I'm just overlooking something simple.

My theme has a homepage template and a blog page template. Sometimes users want to use the blog page as the homepage instead.

When users set that blog template as the front page (Settings » Reading » Static Page as.. » Blog) the pagination stops working. When you click the Next Page button, it goes to the next page, but shows the same posts as the first page. When the blog page is NOT used as the front page, the pagination works fine. Very odd.

This is the contents of my blog page:


<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=5&cat='.get_option('prototype_blog_cat'));
?>

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

<div class="post">

// post content here //

</div><!--end post-->

<?php endwhile; ?>

<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>

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


Look forward to hearing your answers!
Mike

Answers (5)

2010-08-02

Utkarsh Kukreti answers:

Try adding this as the first line

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


Mike McAlister comments:

Where exactly, Utkarsh?


Utkarsh Kukreti comments:

Before

$temp = $wp_query;


Mike McAlister comments:

Hmm, that one didn't work. Didn't seem to have any effect.


Utkarsh Kukreti comments:

Could you link me to the site? (or message me from my profile here)

2010-08-02

Rashad Aliyev answers:

Other solutions: You can use the famous WP-PageNavi plugin.

http://wordpress.org/extend/plugins/wp-pagenavi/

best regards,


Mike McAlister comments:

I did try that previously, but that was also not working properly. I don't want to rely on a plugin though. I would like this to work out of the box.


Rashad Aliyev comments:

Install it WP-PageNavi plugin


use the query like this.

That mean: not showing cat id:1 , and shows 5 posts.

<?php query_posts("cat=-1&showposts=5&paged=$paged");?>




then add this code where you want.

It'll show your navigation truly.
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

Still you've problem or questions, feel free to contact.


Mike McAlister comments:

Maybe if all else fails I will try the plugin again. But like I said, it's very important that the theme works out of the box, without that plugin.


Rashad Aliyev comments:

Install PageNavi plugin and change this check it again.



<div class="navigation">

<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>

</div>


and also I'll try to solve it without plugins.


Rashad Aliyev comments:


Use this codes istead of yours.

It's worked with WP-PageNavi plugin very well!






<?php

$temp = $wp_query;

$wp_query= null;

$wp_query = new WP_Query('paged='.$paged.'&showposts=5&cat='.get_option('prototype_blog_cat'));
while ($wp_query->have_posts()) : $wp_query->the_post();
?>


<div class="post">



// post content here //



</div><!--end post-->

<?php
endwhile;
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
$wp_query = null; $wp_query = $temp; ?>




Rashad Aliyev comments:

For using defaul pagination read this references;
http://codex.wordpress.org/Function_Reference/get_query_var

2010-08-02

Chris Olbekson answers:

Here is a very reliable numeric pagination function you can add to functions.php. Then in your templates just add this line right after <?php endwhile; ?>

<?php numeric_pagination(); ?>

For the blog template add this right before the loop:
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("showposts=6&paged=$page");
?>



Here is the numeric pagination function:

/* Numeric Pagination ********************************************/

function numeric_pagination ($pageCount = 9, $query = null) {

if ($query == null) {
global $wp_query;
$query = $wp_query;
}

if ($query->max_num_pages <= 1) {
return;
}

$pageStart = 1;
$paged = $query->query_vars['paged'];

// set current page if on the first page
if ($paged == null) {
$paged = 1;
}

// work out if page start is halfway through the current visible pages and if so move it accordingly
if ($paged > floor($pageCount / 2)) {
$pageStart = $paged - floor($pageCount / 2);
}

if ($pageStart < 1) {
$pageStart = 1;
}

// make sure page start is
if ($pageStart + $pageCount > $query->max_num_pages) {
$pageCount = $query->max_num_pages - $pageStart;
}

?>
<div id="archive_pagination">
<?php
if ($paged != 1) {
?>
<a href="<?php echo get_pagenum_link(1); ?>" class="numbered page-number-first"><span>&lsaquo; <?php _e('Newest', 'theme'); ?></span></a>
<?php
}
// first page is not visible...
if ($pageStart > 1) {
//echo 'previous';
}
for ($p = $pageStart; $p <= $pageStart + $pageCount; $p ++) {
if ($p == $paged) {
?>
<span class="numbered page-number-<?php echo $p; ?> current-numeric-page"><?php echo $p; ?></span>
<?php } else { ?>
<a href="<?php echo get_pagenum_link($p); ?>" class="numbered page-number-<?php echo $p; ?>"><span><?php echo $p; ?></span></a>

<?php
}
}
// last page is not visible
if ($pageStart + $pageCount < $query->max_num_pages) {
//echo "last";
}
if ($paged != $query->max_num_pages) {
?>
<a href="<?php echo get_pagenum_link($query->max_num_pages); ?>" class="numbered page-number-last"><span><?php _e('Oldest', 'theme'); ?> &rsaquo;</span></a>
<?php } ?>

</div>

<?php } ?>

2010-08-02

juan manuel incaurgarat answers:

try putting this just before $wp_query->have_posts(), consider putting the query on a variable after the while loop:

<?php $page = (get_query_var(’paged’)) ? get_query_var(’paged’) : 1;
query_posts(”showposts=4&paged=$page”); ?>

2010-08-02

wjm answers:

i tracked down the problem, and i am not sure whethere this is a wordpress bug.
$paged (it doesnt matter if you make it global and access via the variable $paged or you use get_query_var('paged') always resolves to 0.
The /page/2 matches the correct rewrite rules.

matched_rule: page/?([0-9]{1,})/?$
matched_query: index.php?&paged=$matches[1]
matched_query (evaluated): &paged=2

but, $paged is 0 no matter what.
trying to track down where there wordpress bug could be.
but i have the feeling its a wordpress bug.


wjm comments:

It has to do with this piece of code.
in wp-includes/query.php

// Correct is_* for page_on_front and page_for_posts
if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
$_query = wp_parse_args($query);
if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
$this->is_page = true;
$this->is_home = false;
$qv['page_id'] = get_option('page_on_front');
// Correct <!--nextpage--> for page_on_front
if ( !empty($qv['paged']) ) {
$qv['page'] = $qv['paged'];
unset($qv['paged']);
}
}
}


for some reason wordpress is "correcting" following pages for front pages.
if you comment this line
//unset($qv['paged']);
your code is likely to work


Mike McAlister comments:

Hi wjm,
This may work, but since it's a theme I am distributing, I can't very well ask that all buyers go into their wp-includes folder to make edits.


wjm comments:

you can fix it by hacking the paged query var.

add this at the beggning of your code

global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}


that solves the issue.


wjm comments:

this is an improved version.

<?php
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}

$temp = $wp_query;

$wp_query= null;

$wp_query = new WP_Query();

$wp_query->query('paged='.$paged.'&showposts=5&cat='.get_option('prototype_blog_cat'));

?>



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



<div class="post">



// post content here //



</div><!--end post-->



<?php endwhile; ?>



<div class="navigation">

<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>

<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>

</div>



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