I have googled the problem, I am worried not only me there are many coder who faces this problem. I applied there fixing but same result. Would you please review my pagination code? I have a static front page, where pagination redirects me to homepage (/
). If I add after url /page/2
manually this one also redirect me to homepage. I know this is childish question but I can’t figure out the problem.
This is the function.
function pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<ul class='pagenavi'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."'>«</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."'>‹</a></li>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li><a href='".get_pagenum_link($i)."' class='current' >".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a></li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged + 1)."'>›</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($pages)."'>»</a></li>";
echo "</ul>n";
}
}
Code used to generate pagination
<?php query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'cat'=> $bcatid, 'posts_per_page' => 9 ) );
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
post content
<?php endwhile; endif; ?>
<?php pagination(); ?>
Thanks in advance for your valuable time.
The correct way to get the current pagination number on a static front page (Page template) you have to use the ‘page’ query variable:
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
More info here: http://codex.wordpress.org/Function_Reference/get_query_var