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

时间:2014-12-19 17:00:13

标签: php wordpress pagination

如何使用以下查询在自定义模板上显示分页?

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

);

另外,我在while循环结束时使用它:

next_posts_link(); 
previous_posts_link(); 

1 个答案:

答案 0 :(得分:0)

  

试试这个

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