如何在自定义WP查询中添加分页

时间:2019-01-04 11:44:29

标签: php wordpress

如何使用自定义wp查询添加自定义分页。这是我的代码。...

<?php

//openion post begin here
$featurePosts = new WP_Query('cat=3&posts_per_page=10&order=DSC&offset=5');

if ($featurePosts->have_posts()) :
while ($featurePosts->have_posts()) : $featurePosts->the_post(); ?>


<?php endwhile;
else :
endif;
wp_reset_postdata();
?>

1 个答案:

答案 0 :(得分:0)

请检查以下代码,这将对您有用。

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;  // get the current page variable and set it          

$featurePosts = new WP_Query( array(
    'cat' => '3',
    'posts_per_page' => 10,
    'paged' => $paged // use $paged variable here
) );


    if ($featurePosts->have_posts()) :
        while ($featurePosts->have_posts()) : $featurePosts->the_post();
         // You code will be here
        endwhile;
    else :
    endif;
    wp_reset_postdata();

  // Below is full code of pagination  

    echo paginate_links( array(
        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
        'total'        => $query->max_num_pages,
        'current'      => max( 1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => true,
        'prev_text'    => sprintf( '<i></i> %1$s', __( 'Next', 'text-domain' ) ),
        'next_text'    => sprintf( '%1$s <i></i>', __( 'Previous', 'text-domain' ) ),
        'add_args'     => false,
        'add_fragment' => '',
    ) );