Wordpress分页 - 不工作(无法访问第二页)

时间:2015-11-08 21:45:54

标签: php wordpress pagination

我昨天在这里写了但是我没有收到任何关于我的问题的答案所以我会再试一次。我的wordpress pagiation有问题,一切正常但我无法访问任何其他页面而不是我的第一页,因为它显示错误,找不到页面。这是我的index.php:

<?php
get_header();?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">


<?php 

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


  $query_args = array(
      'post_type' => 'all_products',
      'posts_per_page' => 2,
      '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(); ?>
      <article class="loop">
        <div class="product">
            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
            <ul>
                <li><strong>Description:</strong> <?php echo(types_render_field( "description_of_the_product", array( 'raw' => true) )); ?></li>
                <li><strong>Type:</strong> <?php echo(types_render_field( "type", array( 'raw' => true) )); ?></li>
                <li><strong>Price:</strong> <?php echo(types_render_field( "price", array( 'raw' => true) )); ?></li>
                <li><strong>Manufacturer:</strong> <?php echo(types_render_field( "manufacturer", array( 'raw' => true) )); ?></li>
                <li><strong>Availability:</strong> <?php echo(types_render_field( "availability:", array( 'raw' => true) )); ?></li>
                <li><strong>Reference:</strong> <?php echo(types_render_field( "reference", array( 'raw' => true) )); ?></li>
                <a href="<?php the_permalink(); ?>">Read more</a>
            <ul>
        <?php the_excerpt(); ?>         
        </div>
      </article>
    <?php endwhile; ?>

    <!-- end of the loop -->

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

<?php next_posts_link( $label , $max_pages ); ?>
  <?php wp_reset_postdata(); ?>

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



<?php get_footer(); ?>

这是functions.php:

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

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   * 
   * It's good because we can now override default pagination
   * in our theme, and use this function in default queries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'index.php/page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

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

}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您无法在查询中使用pagepaged。对于分页,您只需要paged。根据关于分页的WP_Query文档,page用于静态页面,而不是一般分页。尝试:

$query_args = array(
    'post_type' => 'all_products',
    'posts_per_page' => 2,
    'paged' => $paged
);