【问题标题】:Wordpress pagination static pageWordpress 分页静态页面
【发布时间】:2015-07-07 09:09:26
【问题描述】:

我正在开发一个 Wordpress 模板,并希望使用静态页面作为主页。我在那个页面上进行了分页,但是当我转到另一个页面时,URL 发生了变化,并且另一个页面的帖子也出现了,但分页的当前页面是相同的。它说它仍在分页中的“...的第 1 页”上。

有人知道如何更改当前页面吗?

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $page,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('«'),
    'next_text'       => __('»'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
      echo "<span class='page-numbers page-num'>Page " . $page . " of " . $numpages . "</span> ";
      echo $paginate_links;
  }

}
<?php 

  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

  $query_args = array(
      'post_type' => 'post',
      'posts_per_page' => 12,
      'paged' => $paged,
      'page' => $paged
    );

  $the_query = new WP_Query( $query_args ); ?>

  <?php if ( $the_query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post();

// Get all posts
                        get_template_part( 'content', 'post' );

endwhile; ?></div>
    <!-- end of the loop -->

   <nav class='paging-navigation'>
		<?php
      if (function_exists(custom_pagination)) {
        custom_pagination($the_query->max_num_pages,"",$paged);
      }
    ?></nav>


  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?></div>

【问题讨论】:

  • remove 'page' => $paged from $query_args
  • 什么都不做.. :(

标签: php wordpress pagination


【解决方案1】:

解决了问题!把这个:

<?php
  if (function_exists(custom_pagination)) {
    custom_pagination($the_query->max_num_pages,"",$paged);
  }

在 wp_reset_postdata 之后,还从 $query_args 中删除了 'page' => $paged,这对我有用。 :)

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多