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

Need pagination for custom post type WordPress

I've tried several variations of code and can't seem to get anything to work.

Here's my custom post type loop:



<?php
$loop = new WP_Query(
array(

'post_type' => 'oils-blends',
'meta_key' => 'type',
'meta_value' => 'oilblend',
'meta_compare' => 'LIKE',
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40
)
);

while ( $loop->have_posts() ) : $loop->the_post();

echo '<div class="everyday-oil-container">';
echo '<div class="everyday-oil-image"><a href="';
the_permalink();
echo '"><img src="';
if( get_field('oil_image') )
the_field('oil_image');
else
the_field('main_image_link');
echo '" alt="';
the_title();
echo '"></a></div>';
echo '<div class="everyday-oil-title" itemscope itemtype="http://schema.org/HealthAndBeautyBusiness"><a href="';
the_permalink();
echo '" class="black-link"><span itemprop="makesOffer">';
the_field('short_name');
echo '</span></a></div></div>';
endwhile;
?>

<?php endwhile; ?>


Can someone show me how to add pagination? Everything I've tried from Google search doesn't work.

Answers (3)

2015-08-26

Arnav Joy answers:

are you using any plugin like wp page navi ?


Kyler Boudreau comments:

No. I need to do this without a plugin if possible.


Arnav Joy comments:

is this done?

2015-08-26

Andrea P answers:

first you need to set the $paged variable that you use into the query args.
so you have to place this above that code:


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


note: if that is the home page, the var is called ('page') rather than ('paged') so you have to tweak that in the above code.


then this is a basic example of how to display pagination links below a custom loop (the trick is that $big var, that's why many times people have problems with pagination in custom loops..):


<?php
$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' => $the_query->max_num_pages
) );
?>



here you can find more info about how to use that function:
https://codex.wordpress.org/Function_Reference/paginate_links


Andrea P comments:

ah! remember that within the paginate_links function, you have to change $the_query with the name of your query (which is $loop in your sample)


Kyler Boudreau comments:

Hey Andrea,

So would I do this:

<code

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$loop = new WP_Query(

array(



'post_type' => 'oils-blends',

'meta_key' => 'type',

'meta_value' => 'oilblend',

'meta_compare' => 'LIKE',

'orderby'=>'title',

'order'=>'ASC',

'posts_per_page' => 40

)

);


</code>

I don't know much PHP.


Andrea P comments:

yes this is fine, and then at the bottom of the loop, after the endwhile you have to add the paginate_links function.

and now I am seeing that you have another endwhile at the very bottom, which should be a endif

like this


if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();


echo '<div class="everyday-oil-container">';

echo '<div class="everyday-oil-image"><a href="';

the_permalink();

echo '"><img src="';

if( get_field('oil_image') )

the_field('oil_image');

else

the_field('main_image_link');

echo '" alt="';

the_title();

echo '"></a></div>';

echo '<div class="everyday-oil-title" itemscope itemtype="http://schema.org/HealthAndBeautyBusiness"><a href="';

the_permalink();

echo '" class="black-link"><span itemprop="makesOffer">';

the_field('short_name');

echo '</span></a></div></div>';

endwhile;

?>

<?php

$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' => $loop->max_num_pages
) );
?>



<?php endif; ?>


Andrea P comments:

have you edited the code in the first question? I thought the args had already the 'paged' arg in it..

anyway, the correct WP query Args should be (assuming that is the very same of the other question):


$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'oils-blends',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'type',
'value' => '"oilblend"',
'compare' => 'LIKE'
),
array (
'key' => 'show_product',
'value' => '"no"',
'compare' => 'NOT LIKE'
)
),
'orderby'=>'title',
'order'=>'ASC',
'posts_per_page' => 40,
'paged' => $paged
)
);


Andrea P comments:

have you managed to make it work?
did my code sort the issue?

cheers!

2015-08-26

Firoja_Imtosupport answers:

Hi,

/*** Where you want to add pagination add below ***/
<?php new_pagination( ($wp_query->max_num_pages)); ?>
/** wp_query will be query variable of your custom post**/

/*** In themes function.php add below ***/
if( !function_exists( 'new_pagination' ) ){
function new_pagination($pages = ''){
global $paged;

if(is_page_template('template-home.php')){
$paged = intval(get_query_var( 'page' ));
}

if(empty($paged))$paged = 1;

$prev = $paged - 1;
$next = $paged + 1;
$range = 2; // only change it to show more links
$showitems = ($range * 2)+1;

if($pages == ''){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){
$pages = 1;
}
}


if(1 != $pages){
echo "<div class='pagination'>";
echo ($paged > 2 && $paged > $range+1 && $showitems < $pages)? "<a href='".get_pagenum_link(1)."' class='real-btn'>&laquo; ".__('First', 'framework')."</a> ":"";
echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."' class='real-btn' >&laquo; ". __('Previous', 'framework')."</a> ":"";

for ($i=1; $i <= $pages; $i++){
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
echo ($paged == $i)? "<a href='".get_pagenum_link($i)."' class='real-btn current' >".$i."</a> ":"<a href='".get_pagenum_link($i)."' class='real-btn'>".$i."</a> ";
}
}

echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."' class='real-btn' >". __('Next', 'framework') ." &raquo;</a> " :"";
echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."' class='real-btn' >". __('Last', 'framework') ." &raquo;</a> ":"";
echo "</div>";
}
}
}