dongyuying1507
dongyuying1507
2014-12-19 17:00

Wordpress - 如何在模板上启用分页?

已采纳

How can I show pagination on a custom template using the following query?

$args = array (
'post_type'              => 'post',
    'posts_per_page'         => '24',
    'pagination'             => true,
    'tax_query' => array(  
    array(  
        'taxonomy' => 'category',  
        'field' => 'term_id',  
        'terms' => $mh_terms
    )  
)  

);

Also, I use this at end of the while loop:

next_posts_link(); 
previous_posts_link(); 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongmu5246 dongmu5246 7年前

    Try this

    <?php
        $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    
        $args = array (
            'post_type' => 'post',
            'posts_per_page' => '24',
            'pagination' => true,
            'paged' => $paged,
            'tax_query' => array(  
            array(  
                'taxonomy' => 'category',  
                'field' => 'term_id',  
                'terms' => $mh_terms
            ) , 
        ),);  
        $wp_query = new WP_Query($args);
    
        while ($wp_query->have_posts()): $wp_query->the_post();
        get_the_title();
        endwhile;
    
        global $wp_query;
    
        $big = 999999999; // need an unlikely integer
    
        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages
        ) );
    ?>
    
    点赞 评论 复制链接分享

相关推荐