dongyuying1507 2014-12-19 17:00
浏览 37
已采纳

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 2014-12-19 17:45
    关注

    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
        ) );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分