简体   繁体   中英

Wordpress - How can I enable pagination on template?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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