简体   繁体   English

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

[英]How to add pagination in custom wp query

How can I add custom pagination with custom wp query. 如何使用自定义wp查询添加自定义分页。 Here is my code.... 这是我的代码。

<?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();
?>

Kindly check below code this will work for you. 请检查以下代码,这将为您工作。

$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' => '',
    ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM