WordPress paginate_links - how to use it?

后端 未结 1 410
独厮守ぢ
独厮守ぢ 2020-12-06 03:26

I want to add a numberic pagination to a page with its child pages. This is the pagination I would want to create (from bootstrap):

相关标签:
1条回答
  • 2020-12-06 04:21

    Pagination Like : Prev 1 2 3 Next

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $data= new WP_Query(array(
        'post_type'=>'YOUR_POST_TYPE', // your post type name
        'posts_per_page' => 3, // post per page
        'paged' => $paged,
    ));
    
    if($data->have_posts()) :
        while($data->have_posts())  : $data->the_post();
                // Your code
        endwhile;
    
        $total_pages = $data->max_num_pages;
    
        if ($total_pages > 1){
    
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,
                'prev_text'    => __('« prev'),
                'next_text'    => __('next »'),
            ));
        }
        ?>    
    <?php else :?>
    <h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
    <?php endif; ?>
    <?php wp_reset_postdata();?>
    

    Could you please try above code? I think it's helpful for you.

    0 讨论(0)
提交回复
热议问题