I have taken the following pagination code from a 'premium' Wordpress theme to try and integrate this in to a custom one, but can't seem to get it to work:
In the blog.php page template, I have the loop:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args=array(
'paged' => $paged,
'post_type' => 'post',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'date',
);
$wp_query = new WP_query($args);
if ( $wp_query -> have_posts() ) :
$j = 1;
while ($wp_query -> have_posts()): $wp_query -> the_post();?>
CONTENT HERE
<?php endwhile;prestigo_SPaginate();endif;?>
And the function for this is:
if(!function_exists('prestigo_SPaginate')){
function prestigo_SPaginate()
{
$currentPage = null;
$totalPage = null;
global $wp_query;
$currentPage = intval(get_query_var('paged'));
if(empty($currentPage))
{
$currentPage = 1;
}
$totalPage = intval(ceil($wp_query->found_posts / intval(get_query_var('posts_per_page'))));
if($totalPage <= 1)
{
return '';
}
$paginateResult = '<!-- PAGINATION -->
<ul class="pagination clearfix">';
if ($currentPage > 1)
{
$paginateResult .= '<li class="pull-left"><a href="'.get_pagenum_link($currentPage - 1).'"><i class="fe arrow_carrot-left"></i></a></li>';
}elseif ($currentPage = 1)
{
$paginateResult .= '<li class="pull-left"><a href="javascript:void(0);"><i class="fe arrow_carrot-left"></i></a></li>';
}
$paginateResult .= prestigo_ListLink(1, $totalPage, $currentPage);
if ($currentPage < $totalPage)
{
$paginateResult .= "<li class='pull-right'><a href='" . get_pagenum_link($currentPage + 1) . "' class='spaginate-next'><i class='fe arrow_carrot-right'></i></a></li>";
}elseif($currentPage = $totalPage){
$paginateResult .= "<li class='pull-right'><span class='spaginate-next'><i class='fe arrow_carrot-right'></i></span></li>";
}
$paginateResult .= "</ul><!-- //PAGINATION -->";
echo $paginateResult;
return $paginateResult;
}
}
if(!function_exists('prestigo_ListLink')){
function prestigo_ListLink($intStart, $totalPage, $currentPage)
{
$pageHidden = '<span class="spaginate-hidden">... </span>';
$linkResult = "";
$hiddenBefore = false;
$hiddenAfter = false;
for ($i = $intStart; $i <= $totalPage; $i++)
{
if($currentPage === intval($i))
{
$linkResult .= '<li class="active"><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
else if(($i <= 6 && $currentPage < 10) || $i == $totalPage || $i == 1 || $i < 4 || ($i <= 6 && $totalPage <= 6) || ($i > $currentPage && ($i <= ($currentPage + 2))) || ($i < $currentPage && ($i >= ($currentPage - 2))) || ($i >= ($totalPage - 2) && $i < $totalPage))
{
$linkResult .= '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
if($i <= 6 && $currentPage < 10)
{
$hiddenBefore = true;
}
}
else
{
if(!$hiddenBefore)
{
$linkResult .= $pageHidden;
$hiddenBefore = true;
}
else if(!$hiddenAfter && $i > $currentPage)
{
$linkResult .= $pageHidden;
$hiddenAfter = true;
}
}
}
return $linkResult;
}
}
Unfortunately the pagination displays but clicking on page number 2 shows the same posts as page number 1.
Any advice would be great, thanks.
I have taken the following pagination code from a 'premium' Wordpress theme to try and integrate this in to a custom one, but can't seem to get it to work:
In the blog.php page template, I have the loop:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args=array(
'paged' => $paged,
'post_type' => 'post',
'posts_per_page' => '1',
'order' => 'DESC',
'orderby' => 'date',
);
$wp_query = new WP_query($args);
if ( $wp_query -> have_posts() ) :
$j = 1;
while ($wp_query -> have_posts()): $wp_query -> the_post();?>
CONTENT HERE
<?php endwhile;prestigo_SPaginate();endif;?>
And the function for this is:
if(!function_exists('prestigo_SPaginate')){
function prestigo_SPaginate()
{
$currentPage = null;
$totalPage = null;
global $wp_query;
$currentPage = intval(get_query_var('paged'));
if(empty($currentPage))
{
$currentPage = 1;
}
$totalPage = intval(ceil($wp_query->found_posts / intval(get_query_var('posts_per_page'))));
if($totalPage <= 1)
{
return '';
}
$paginateResult = '<!-- PAGINATION -->
<ul class="pagination clearfix">';
if ($currentPage > 1)
{
$paginateResult .= '<li class="pull-left"><a href="'.get_pagenum_link($currentPage - 1).'"><i class="fe arrow_carrot-left"></i></a></li>';
}elseif ($currentPage = 1)
{
$paginateResult .= '<li class="pull-left"><a href="javascript:void(0);"><i class="fe arrow_carrot-left"></i></a></li>';
}
$paginateResult .= prestigo_ListLink(1, $totalPage, $currentPage);
if ($currentPage < $totalPage)
{
$paginateResult .= "<li class='pull-right'><a href='" . get_pagenum_link($currentPage + 1) . "' class='spaginate-next'><i class='fe arrow_carrot-right'></i></a></li>";
}elseif($currentPage = $totalPage){
$paginateResult .= "<li class='pull-right'><span class='spaginate-next'><i class='fe arrow_carrot-right'></i></span></li>";
}
$paginateResult .= "</ul><!-- //PAGINATION -->";
echo $paginateResult;
return $paginateResult;
}
}
if(!function_exists('prestigo_ListLink')){
function prestigo_ListLink($intStart, $totalPage, $currentPage)
{
$pageHidden = '<span class="spaginate-hidden">... </span>';
$linkResult = "";
$hiddenBefore = false;
$hiddenAfter = false;
for ($i = $intStart; $i <= $totalPage; $i++)
{
if($currentPage === intval($i))
{
$linkResult .= '<li class="active"><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
else if(($i <= 6 && $currentPage < 10) || $i == $totalPage || $i == 1 || $i < 4 || ($i <= 6 && $totalPage <= 6) || ($i > $currentPage && ($i <= ($currentPage + 2))) || ($i < $currentPage && ($i >= ($currentPage - 2))) || ($i >= ($totalPage - 2) && $i < $totalPage))
{
$linkResult .= '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
if($i <= 6 && $currentPage < 10)
{
$hiddenBefore = true;
}
}
else
{
if(!$hiddenBefore)
{
$linkResult .= $pageHidden;
$hiddenBefore = true;
}
else if(!$hiddenAfter && $i > $currentPage)
{
$linkResult .= $pageHidden;
$hiddenAfter = true;
}
}
}
return $linkResult;
}
}
Unfortunately the pagination displays but clicking on page number 2 shows the same posts as page number 1.
Any advice would be great, thanks.
Share Improve this question edited Jul 13, 2017 at 8:56 Adrian asked Sep 15, 2014 at 12:48 AdrianAdrian 3642 gold badges8 silver badges25 bronze badges3 Answers
Reset to default 3After struggling for hours on this (and 6 years after the question was asked), for me the solution was lying within the $paged parameter:
I changed
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
to
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
And then it worked!
try changing the paged
parameter and query var; I've experienced in the past the variable being different than what the Codex says. try switching paged
with page
in both the get_query_var
and the WP_Query
args.
For pagination these days I use the following:
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}