Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)

share|improve this question
    
Have you tried rilwis's comment about using query_posts() instead of get_pages() on the link you provided? –  kevin Jan 10 '12 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. –  JohnnyPea Jan 10 '12 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 Jan 11 '12 at 12:44

1 Answer 1

up vote 4 down vote accepted
+50

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

<?php

// 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
        setup_postdata($post);
        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
wp_reset_postdata();

?>
share|improve this answer
    
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 Jan 13 '12 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. –  JohnnyPea Jan 13 '12 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 Sep 28 '12 at 20:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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