Skip to content
Advertisement

Pagination of results of wp_list_pages [closed]

I’m displaying the child pages of a page with this code:

<ul class="llista-residus">
    <?php
    global $id;
    wp_list_pages( array(
        'title_li'    => '',
        'child_of'    => $id,
        'show_date'   => 'modified',
        'date_format' => $date_format,
    ) );
    ?>
</ul>

I have a lot of results, so I would like to display them in different pages, with an alphabetical pagination. I’ve tried different solutions but not working (my notions of php are very basic…). Any idea?

Thank you

EDIT:

Finally I tried it modifying this code: https://wordpress.stackexchange.com/questions/38188/pagination-in-a-custom-list-created-with-get-pages-function

The result:

<?php
$thispost = $post->ID;
// Posts Per Page option
$ppp = 30;

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

$pages = get_posts( $args );

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

$pages = get_pages('child_of='.$thispost);
$count_pages = ceil (count($pages) / $ppp);

// display the navigation
if ( $count_pages > 0 ) {
    echo '<div> Pàg. ';
    for ($i = 1; $i <= $count_pages; $i++) {
        $separator = ( $i < $count_pages ) ? ' | ' : '';
        $url_args = add_query_arg( 'list', $i );
        echo  "<a href='$url_args'>$i</a>".$separator;
    }
    echo '</div>';
}

wp_reset_postdata();

?>  

I don’t know if it is the more correct way, but it worked.

Advertisement

Answer

The docs says that

you can sort your results using sort_column.

I didn’t see a way to do paging using this function, but luckily you can use get_pages for this purpose. With sort_order you can specify your direction of sorting. offset and number can be used to have a paging. So, if you read the relevant parts of the links I have provided in this answer, you should be able to solve the issue. If not, then please let me know.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement