How to use the_posts_navigation for wp_query and get_posts?

Question

WordPress have the_posts_navigation function since 4.1.0. But I don’t know how to use with wp_query or get_posts. the following code is in a template file of page.
wp_query method:

    <?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;
    }

    $get_posts=new wp_query('post_type=case&posts_per_page=2&paged='.$paged);

    while($get_posts->have_posts()):$get_posts->the_post();
    the_title();
    endwhile;

    the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'cm' ),
        'next_text'          => __( 'Next page', 'cm' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
    ) );
    ?>

get_posts method:

    <?
    while(have_posts()):the_post();
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
        $paged = get_query_var('page');
        } else {
        $paged = 1;
    }

    $case_posts=get_posts('post_type=case&posts_per_page=2&paged='.$paged);
    echo '<pre>';
    print_r($case_posts);
    echo '</pre>';
    foreach($case_posts as $case_post){
        echo $case_post->post_title;
    }
    endwhile;
    // Previous/next page navigation.
    the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'cm' ),
        'next_text'          => __( 'Next page', 'cm' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
    ) );
    ?>

They don’t work and display pagination, but entering http://127.0.0.1/gdboer/?page_id=74&page=2 manually in address bar, it works.
Who can help me, thank you so much!

0
, , Vincent Wong 7 years 2015-01-14T20:09:12-05:00 0 Answers 100 views 0

Leave an answer

Browse
Browse