如何分页Wordpress自定义帖子tipe循环

时间:2015-11-04 06:15:50

标签: php wordpress pagination

以前曾提出类似的问题,但它对我来说根本不起作用: How to paginate a custom WP_query() loop - Wordpress

我有一个循环我的媒体文件工作正常,但我似乎无法弄清楚如何正确分页。它基本上是一个在网格中输出我的图像的循环。

到目前为止,我已设法回复“加载旧条目”,但当我点击它转到第二页时,它在localhost / myweb / page / 2 /

显示404

这是我目前的代码:

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
  $query_args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'orderby' => 'rand',
    'order' => 'desc',
    'post_status'    => 'inherit',
    'posts_per_page' => 60,
    'paged' => $paged
  );
  $the_query = new WP_Query( $query_args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
  <?php
    $image = wp_get_attachment_image_src( get_the_ID(), 'large' );
    $things = '<img src="' . $image[0] . '">';
    $imgurl = wp_get_attachment_link( $attachment->ID, '' , true, false, $things );
    echo '<li>' . $imgurl . '</li>';?>
<?php endwhile; ?>
</ul>

<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
  <nav class="prev-next-posts">
    <div class="prev-posts-link">
      <?php echo get_next_posts_link( 'Older entries', $the_query->max_num_pages ); // display older posts link ?>
    </div>
    <div class="next-posts-link">
      <?php echo get_previous_posts_link( 'Newer Entries' );?>
    </div>
  </nav>
<?php } ?>

0 个答案:

没有答案