如何在Wordpress的自定义模板页面中插入分页

时间:2018-07-13 11:44:17

标签: wordpress pagination

我想向我的WordPress博客主题添加分页 但我不能使用paginate_linksnext_posts_linkspreveius_posts_links 这是我的查询

<h1>posts</h1>
<?php
    $our_current_page = get_query_var('paged');
$args = array(
        'post_type'         => 'post',
        'posts_per_page' => get_option('posts_per_page'),
        'order' => 'DESC',
        'paged' => $our_current_page
    );
        $my_query = new WP_Query($args);
        while ($my_query->have_posts()):
            $my_query->the_post();
            $mypost = $post->ID;
            ?>

          <?php echo the_post_thumbnail('thumbnail',array('class'=>'img-rounded'));?>  <h2><a href="<?php echo get_permalink();
 ?>"><?php the_title(); ?></a></h2>
 <small><?php echo get_the_date() . " | ";
 $mycats = get_the_category();
 $cats = array();
 for($i = 0; $i < count($mycats) - 1; $i++){
     $cats[$i] = $mycats[$i]->name;
 }
 echo implode(" - ",$cats). " | " . get_the_author_meta('display_name');
 ?>
 </small>
 <hr>
    <?php update_post_meta( get_the_id(), '_visibility', 'visible' ) ?>
            <?php endwhile; wp_reset_postdata(); ?>
</div>
<br>
<br>
<!-- here i want my pagination -->
<br>


请帮助我做到这一点,有没有插件可以帮助我在模板中使用分页?

1 个答案:

答案 0 :(得分:0)

请替换下面将有帮助的代码

<h1>posts</h1>
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$args = array(
    'post_type'         => 'post',
    'posts_per_page' => get_option('posts_per_page'),
    'order' => 'DESC',
    'paged' => $paged
);
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()):
        $my_query->the_post();
        $mypost = $post->ID;
        ?>

      <?php echo the_post_thumbnail('thumbnail',array('class'=>'img-rounded'));?>  <h2><a href="<?php echo get_permalink();?>"><?php the_title(); ?></a></h2>
      <small><?php echo get_the_date() . " | ";
     $mycats = get_the_category();
     $cats = array();
     for($i = 0; $i < count($mycats) - 1; $i++){
     $cats[$i] = $mycats[$i]->name;
    }
   echo implode(" - ",$cats). " | " . get_the_author_meta('display_name');
   ?>
   </small>
   <hr>
<?php update_post_meta( get_the_id(), '_visibility', 'visible' ) ?>
        <?php endwhile; 
        wp_reset_postdata(); 
    if( $my_query->max_num_pages > 1){
        $my_query->query_vars['paged'] > 1 ? $current = $my_query->query_vars['paged'] : $current = 1;
            echo paginate_links( array(
                'base' => str_replace( '999999999', '%#%', esc_url( get_pagenum_link( '999999999' ) ) ),
                'format'       => '',
                'add_args'     => '',
                'current'      => $current,
                'total'        => $my_query->max_num_pages,
                'prev_text'    => '<i class="fas fa-angle-left"></i>',
                'next_text'    => '<i class="fas fa-angle-right"></i>',
                'type'         => 'plain',
                'end_size'     => 3,
                'mid_size'     => 3
            ) );
    }
        ?>
</div>
<br>
<br>
<!-- here i want my pagination -->
<br>