首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress分页-无法工作(无法访问第二页)

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

Stack Overflow用户
提问于 2015-11-09 05:45:54
回答 1查看 1.7K关注 0票数 0

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

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>";
  }

}

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2015-11-09 06:08:52

不能在查询中使用page和paged。对于分页,您只需要paged。根据有关分页的WP_Query文档,page用于静态页面,而不是通用分页。尝试:

代码语言:javascript
复制
$query_args = array(
    'post_type' => 'all_products',
    'posts_per_page' => 2,
    'paged' => $paged
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33599385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档