How to include pagination in a Wordpress Custom Post Type Query

后端 未结 2 863
太阳男子
太阳男子 2020-12-01 05:40

I have the code below:



 have_posts())          


        
相关标签:
2条回答
  • 2020-12-01 06:16

    When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.

    Example of a custom query:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
        'post_type'=>'post', // Your post type name
        'posts_per_page' => 6,
        'paged' => $paged,
    );
    
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
    
                 // YOUR CODE
    
        endwhile;
    
        $total_pages = $loop->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 »'),
            ));
        }    
    }
    wp_reset_postdata();
    ?>
    

    Example of paginate_links parameters adapted to the custom query above:

    For more reference please visit this link

    0 讨论(0)
  • 2020-12-01 06:30

    Try the code below:

        $the_query = new WP_Query( array('posts_per_page'=>30,
                                     'post_type'=>'phcl',
                                     'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                                ); 
                                ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <div class="col-xs-12 file">
    <a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
    <i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
    </a>
    <div class="file-description"><?php the_content(); ?></div>
    </div>
    <?php
    endwhile;
    
    $big = 999999999; // need an unlikely integer
     echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
    
    wp_reset_postdata();
    
    0 讨论(0)
提交回复
热议问题