327 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Sold between 50 000 and 100 000 dollars
  • United Kingdom
PaulWinslow says

Hi guys. I’m looping a custom post-type in a custom page template – what’s the cleanest way to have numbered pagination here? No plugins.

Cheers!

1090 posts The Last Man Standing
  • Elite Author
  • Author had a Free File of the Month
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Exclusive Author
  • Bought between 10 and 49 items
  • Sold between 50 000 and 100 000 dollars
  • Beta Tester
ImanGM says

Hey Paul,

I wish this will help you :)

<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
$current_page = $paged;

$arg = array ( 
    'posts_per_page'        => any number,
    'orderby'                => order by anything,
    'order'                    => desc or asc?,
    'post_type'               => your post type goes here,
    'paged'                    => $paged,
    'ignore_sticky_posts'    => 1
);

//Start The WP Query
$posts_query = new WP_Query($arg);

$pagination_text =  paginate_links(
    array(  
        'base'         => @add_query_arg( $paged_var, '%#%' ),
        'format'       => '',  
        'current'      => $current_page,  
        'total'        => $posts_query->max_num_pages, 
        'show_all'     => false,
        'end_size'     => 1,
        'mid_size'     => 5,
        'prev_next'    => true,
        'prev_text'    => '«',  
        'next_text'    => '»',
        'type'         => 'plain'
    )
);

// Put your loop here

echo $pagination_text;
wp_reset_query();
?>
321 posts
  • Envato Studio (Microlancer) Beta Tester
  • Most Wanted Bounty Winner
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Bought between 10 and 49 items
  • Referred between 50 and 99 users
  • Romania
QBKL says

The paginate_links() function is your best bet, also used in imangm’s example.

WP Codex: http://codex.wordpress.org/Function_Reference/paginate_links

327 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Sold between 50 000 and 100 000 dollars
  • United Kingdom
PaulWinslow says

Hey Paul,

I wish this will help you :)

<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
$current_page = $paged;

$arg = array ( 
    'posts_per_page'        => any number,
    'orderby'                => order by anything,
    'order'                    => desc or asc?,
    'post_type'               => your post type goes here,
    'paged'                    => $paged,
    'ignore_sticky_posts'    => 1
);

//Start The WP Query
$posts_query = new WP_Query($arg);

$pagination_text =  paginate_links(
    array(  
        'base'         => @add_query_arg( $paged_var, '%#%' ),
        'format'       => '',  
        'current'      => $current_page,  
        'total'        => $posts_query->max_num_pages, 
        'show_all'     => false,
        'end_size'     => 1,
        'mid_size'     => 5,
        'prev_next'    => true,
        'prev_text'    => '«',  
        'next_text'    => '»',
        'type'         => 'plain'
    )
);

// Put your loop here

echo $pagination_text;
wp_reset_query();
?>

I ran something very similar and it does not correctly move onto page 2. This exact code doesn’t seem to do the trick, either.

1090 posts The Last Man Standing
  • Elite Author
  • Author had a Free File of the Month
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Exclusive Author
  • Bought between 10 and 49 items
  • Sold between 50 000 and 100 000 dollars
  • Beta Tester
ImanGM says

I had the same issue at first. These lines are important to fix second page that is not working:

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

and

'base' => @add_query_arg( $paged_var, '%#%' ),

Did you try this?
327 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Sold between 50 000 and 100 000 dollars
  • United Kingdom
PaulWinslow says

It just isn’t working. Page 2 just shows the same posts from Page 1.

This paging shit always gets me.

279 posts
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Has been a member for 1-2 years
  • Bought between 10 and 49 items
  • Referred between 10 and 49 users
PrimaThemes says

It just isn’t working. Page 2 just shows the same posts from Page 1.

If paginate_links already show some page links for you with correct total pages also, then this function works properly.

If your “page 2” still show same posts from “page 1”, it is different issue, you can check these:

(1) Permalink issue, you can go to Settings – Permalinks, and save it again. If your issue is only a permalink issue, you should see “page 2” content now.

(2) Missing “paged” parameter on your custom query. If you want your custom query support pagination, you need to add “paged” parameter on your custom query, for example:

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

$args = array(
    'posts_per_page' => 5,
    'post_type' => 'portfolio',
    'paged' => $paged,
);

$the_query = new WP_Query( $args );

If you have checked two points above and still get this issue, it is better if you post complete code of your custom page template here.

203 posts
  • Beta Tester
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Referred between 100 and 199 users
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
biyanpasau says

It just isn’t working. Page 2 just shows the same posts from Page 1. This paging shit always gets me.

You must wrap the loop with a new query. I use this:

<?php if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
query_posts( array( 'post_type' => 'portfolio', 'posts_per_page' => '4', 'paged' => $paged ) ); ?>

**********LOOP HERE*************

<?php wp_reset_postdata();  // Reset ?>
327 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Sold between 50 000 and 100 000 dollars
  • United Kingdom
PaulWinslow says

I still can’t get this to work…

http://snippi.com/s/fm2dud8

This time I’m not querying a custom post-type, these are just standard posts in a magazine theme..

This loop is in a custom page-template which is set as the static front-page..

The page links are there but it does not go to page 2.. it shows the same posts as page 1 and the “1” link is still active..

Any help?

Cheers!

158 posts Lord of the SEO Realm
  • Contributed a Blog Post
  • Envato Staff
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Australia
  • Referred between 200 and 499 users
  • Has been a member for 2-3 years
  • Bought between 10 and 49 items
SaijoGeorge Staff says
by
by
by
by
by
by