329 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 100+ items on Envato Market
+2 more
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!

1245 posts The Last Man Standing
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Had an item featured on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has been a beta tester for an Envato feature
+7 more
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();
?>
360 posts
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+5 more
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

329 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 100+ items on Envato Market
+2 more
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.

1245 posts The Last Man Standing
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Had an item featured on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has been a beta tester for an Envato feature
+7 more
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?
329 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 100+ items on Envato Market
+2 more
PaulWinslow says

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

This paging shit always gets me.

280 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
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.

204 posts
  • Has referred 100+ members
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
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 ?>
329 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 100+ items on Envato Market
+2 more
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!

159 posts Lord of the SEO Realm
  • Has referred 200+ members
  • Has collected 10+ items on Envato Market
  • Member of the Envato Team
  • Sells items exclusively on Envato Market
+5 more
SaijoGeorge Envato team says
by
by
by
by
by
by