381 posts Simple is the new black.
  • Weekly Top Seller
  • Elite Author
  • Author Level 7
  • 5 Years of Membership
+5 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!

1891 posts The Last Man Standing
  • Weekly Top Seller
  • Author Level 8
  • Beta Tester
  • Moderator
+10 more
ImanGM
Moderator
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();
?>
554 posts
  • Bundle Boss
  • Most Wanted Winner
  • Author Level 5
  • 6 Years of Membership
+4 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

381 posts Simple is the new black.
  • Weekly Top Seller
  • Elite Author
  • Author Level 7
  • 5 Years of Membership
+5 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.

1891 posts The Last Man Standing
  • Weekly Top Seller
  • Author Level 8
  • Beta Tester
  • Moderator
+10 more
ImanGM
Moderator
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?
381 posts Simple is the new black.
  • Weekly Top Seller
  • Elite Author
  • Author Level 7
  • 5 Years of Membership
+5 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
  • Affiliate Level 2
  • Author Level 6
  • Collector Level 2
  • Exclusive Author
+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.

210 posts Kita harus putus
  • Author Level 7
  • Featured Author
  • Beta Tester
  • Elite Author
+4 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 ?>
381 posts Simple is the new black.
  • Weekly Top Seller
  • Elite Author
  • Author Level 7
  • 5 Years of Membership
+5 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!

171 posts Lord of the SEO Realm
  • Affiliate Level 5
  • Collector Level 2
  • Envato Team
  • Exclusive Author
+5 more
SaijoGeorge
Envato team
says
by
by
by
by
by
by