I have a custom page that calls a function - say generatelist

the generatelist functions call get_pages() and loops through the array using for each and building a list containing various data from the meta data of the pages (yes pages, not posts! for a reason)

the functions returns the list (actually a catalog of pages) and echoes it into the content area of the custom page.

since the list is becoming longer and longer (as more purchase pages are added) I want to add pageination to the list (while keeping the rest of the page, including custom pages that comes after the list and of course the rest of the theme:footer,sidebar etc)

I saw PageNavi plugin but didn't figure out where should I use the template tag, and how do I control the number of entries before pagination.

this question is similiar to this one but that didn't really get an answer.

be glad to any help with this (actually I'm not a php guy, more of a python type, but built this plugin to help a friend and now need to scale this up)

  • Have you tried rilwis's comment about using query_posts() instead of get_pages() on the link you provided?
    – kevin
    Commented Jan 10, 2012 at 20:36
  • @kevin I think this would paginate all the page including the main loop. Correct me if I am wrong. On the other hand it could be probably used on single pages because there is only one page in the loop. But you have to still some other plugin or code to make your pagination numbered. Commented Jan 10, 2012 at 21:20
  • @JohnnyPea, you can choose a lot of options to customize the query and amongst them, the post_type, which in your case would be 'page'. You can give a look at the codex here for more details: codex.wordpress.org/Class_Reference/… Tell me if it helps.
    – kevin
    Commented Jan 11, 2012 at 12:44

1 Answer 1


I suggest using get_posts because you are able to set "paged" parameter there.


// Posts Per Page option
$ppp = 3;

// find out on which page are we
$paging =  isset( $_GET['list'] ) && ! empty( $_GET['list'] ) ? $_GET['list'] : 1 ;
// arguments for listed pages
$args = array(
    'posts_per_page' => $ppp,
    'post_type' => 'page',
    'paged' => $paging,

$pages = get_posts( $args );

if ( count( $pages ) > 0 ) {
    echo '<ul>';
    foreach ( $pages as $post ) {
        // http://codex.wordpress.org/Function_Reference/setup_postdata
        echo '<li><a href="'.get_permalink( $post->ID ).'">'.$post->post_title.'</a></li>';
    echo '</ul>';
} else {
    echo '<p>No pages!</p>';

$args = array(
    // set arguments for your pages here as well but be aware some parameters are different! http://codex.wordpress.org/Function_Reference/get_pages
    // or you can use http://codex.wordpress.org/Template_Tags/get_posts instead and exclude the "paged" argument

// how many pages do we need?
$count_pages = ceil( count( get_pages($args) ) / $ppp );

// display the navigation
if ( $count_pages > 0 ) {
    echo '<div>';
    for ($i = 1; $i <= $count_pages; $i++) {
        $separator = ( $i < $count_pages ) ? ' | ' : '';
        // http://codex.wordpress.org/Function_Reference/add_query_arg
        $url_args = add_query_arg( 'list', $i );
        echo  "<a href='$url_args'>Page $i</a>".$separator;
    echo '</div>';

// http://codex.wordpress.org/Function_Reference/wp_reset_postdata

  • thanks @JohnnyPea I'll try that - but a Question first: since the whole function supposed to return to the page and echo there (in the page - echo "generate_list()") where should I call wp_reset_postdata()? after calling the function in the page and echoeing the result ul and div or in the function before returning the ul and div?
    – alonisser
    Commented Jan 13, 2012 at 9:58
  • @alonisser You can wrap whole code as it is in your function just store your result in some variable which should be returned (return $yourvariable;) after the wp_reset_postdata() function - it should be placed somewhere AFTER the looping through the data. Commented Jan 13, 2012 at 16:14
  • Only downside to get_posts is that you can't use a hierarchical argument to list the pages in their respective menu_order.
    – AlxVallejo
    Commented Sep 28, 2012 at 20:56
  • Instead of the pagination using URL parameters, is it possible we can use the same format that Wordpress use for pagination links? ie. /page/2/, /page/3/ etc?
    – Lee
    Commented Aug 17, 2016 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.