为什么分页不起作用并在 wordpress 网站上出现 404 错误?

美好的一天!问题是:在模板类别(存档)中,当您单击 404 错误的第 2 页时,分页不起作用.不明白怎么解决的请大家帮忙,头都坏了

我的循环:

get_queried_object_id(),'post_type'='post','posts_per_page'=>9,//'order'=>'desc','分页' =>$分页,);$query = new WP_query($arg);if($query->have_posts()) : ?>
<?phpecho '<div class="row">';$i=0;$formcreated=false;while( $query->have_posts() ) :$query->the_post();//显示帖子终了;wp_reset_postdata();万一;?><div class="分页"><?php如果(function_exists('custom_pagination')){custom_pagination($query->max_num_pages,"",$paged);}?><?php wp_reset_postdata();?>

还有我的自定义分页:

function my_post_queries( $query ) {//不要更改 wp-admin 页面上的查询,只有在它是主查询时才更改它if (!is_admin() && $query->is_main_query()){//更改主页和类别页面的查询如果(is_category()){$query->set('posts_per_page', 1);$query->set('post_type','product');}}}add_action('pre_get_posts', 'my_post_queries');函数 custom_pagination($numpages = '', $pagerange = '', $paged='') {如果(空($pagerange)){$pagerange = 2;}/*** 我们函数的第一部分是回退* 用于常规循环内的自定义分页* 使用全局 $paged 和全局 $wp_query 变量.** 很好,因为我们现在可以覆盖默认分页* 在我们的主题中,并在默认查询中使用此功能* 和自定义查询.*/全局 $paged;如果(空($paged)){$paged = 1;}if ($numpages == '') {全局 $wp_query;$numpages = $wp_query->max_num_pages;如果(!$numpages){$numpages = 1;}}/*** 我们构造分页参数以进入我们的 paginate_links* 功能.*/$pagination_args = 数组('基础' =>get_pagenum_link(1) .'%_%','格式' =>'页/%#%','总计' =>$numpages,'当前' =>$分页,'show_all' =>错误的,'end_size' =>1、'mid_size' =>$pagerange,'prev_next' =>真的,'prev_text' =>__('&#60;'),'next_text' =>__('&#62;'),'类型' =>'清楚的','add_args' =>错误的,'add_fragment' =>'');$paginate_links = paginate_links($pagination_args);如果($ paginate_links){echo "<nav class='custom-pagination'>";回声 $paginate_links;echo "</nav>";}}

解决方案

由于最近在两个不同的论坛上出现了这个问题,我正在回答这个问题.

如果您使用自定义分页,例如您使用的似乎来自 http://callmenick.com/post/custom-wordpress-loop-with-pagination 但它也发生在 Genesis 子主题中,因为父编号的分页是人们所需要的.

为什么会得到 404 页面? callmenick.com 和 Genesis (genesis_posts_nav) 中的自定义分页用于 ma​​in 查询,因此如果您的分页用于不同的查询是在您的阅读设置(为主要查询设置)中的每页帖子下,然后您将在第 2 页上看到 404.

<块引用>

WordPress 网站上的每个前端页面请求都会生成一个主页面询问.WordPress 决定加载的模板基于该主查询的结果(您可以看到 WordPress 执行的顺序这些事情通过查看操作参考页面).尽管事实上,你从来没有输出那个查询的结果,它仍然在运行,对于分页档案,这是一个问题,如果你是尝试将该分页用于不同的查询.— Milo https://wordpress.stackexchange.com/a/120963/64742

您不会经常看到这个问题,因为很多人只是为该循环构建分页,而不是从 functions.php 文件或父主题中重新使用它.您可以在此处了解:https://codex.wordpress.org/Function_Reference/paginate_links>

让我们从头开始,每当你编码时,打开 wp-config.php 中的调试


我的 cpt 存档中的基本自定义循环.

archive-product.php

 <?php $paged = ( get_query_var('paged') ) ?get_query_var('paged') : 1;$product_args = 数组('post_type' =>'产品','posts_per_page' =>2,//和我们functions.php文件中的parse_query过滤器一样'分页' =>$分页,'页面' =>$分页);$product_query = new WP_Query( $product_args);?><?php if ( $product_query->have_posts() ) : ?><!-- 循环--><?php while ( $product_query->have_posts() ) : $product_query->the_post();?><文章类=循环"><h3><?php the_title();?></h3><div class="content"><?php the_excerpt();?>

</文章><?php endwhile;?><!-- 循环结束--><!-- 此处分页--><?php如果(function_exists('custom_pagination')):custom_pagination( $product_query->max_num_pages,"",$paged );万一;?><?php wp_reset_postdata();?><?php 其他:?><p><?php _e('抱歉,没有符合您条件的帖子.');?></p><?php endif;?>


在您的functions.php文件中:

了解条件.https://codex.wordpress.org/Conditional_Tagshttps://codex.wordpress.org/Function_Reference/is_post_type_archive

/*** CPT 存档的每页帖子数* 防止 404 如果在主查询上每页发帖* 大于产品 cpt 存档的每页帖子数** 感谢 https://sridharkatakam.com/提供改进的解决方案!*/函数 prefix_change_cpt_archive_per_page( $query ) {//* 用于 cpt 或任何帖子类型的主存档if ( $query->is_main_query() && !is_admin() && is_post_type_archive( 'product' ) ) {$query->set('posts_per_page', '2');}}add_action('pre_get_posts', 'prefix_change_cpt_archive_per_page');/**** CPT 存档下的类别(测试类别)的每页帖子数**/函数 prefix_change_category_cpt_posts_per_page( $query ) {if ( $query->is_main_query() && !is_admin() && is_category( 'test-category' ) ) {$query->set('post_type', array('product'));$query->set('posts_per_page', '2');}}add_action('pre_get_posts', 'prefix_change_category_cpt_posts_per_page');/**** 自定义编号分页* @http://callmenick.com/post/custom-wordpress-loop-with-pagination**/函数 custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {如果(空($pagerange)){$pagerange = 2;}/*** 我们函数的第一部分是回退* 用于常规循环内的自定义分页* 使用全局 $paged 和全局 $wp_query 变量.** 很好,因为我们现在可以覆盖默认分页* 在我们的主题中,并在默认查询中使用此功能* 和自定义查询.*/全局 $paged;如果(空($paged)){$paged = 1;}if ($numpages == '') {全局 $wp_query;$numpages = $wp_query->max_num_pages;如果(!$numpages){$numpages = 1;}}/*** 我们构造分页参数以进入我们的 paginate_links* 功能.*/$pagination_args = 数组('基础' =>get_pagenum_link(1) .'%_%','格式' =>'页/%#%','总计' =>$numpages,'当前' =>$分页,'show_all' =>错误的,'end_size' =>1、'mid_size' =>$pagerange,'prev_next' =>真的,'prev_text' =>__('«'),'next_text' =>__('»'),'类型' =>'清楚的','add_args' =>错误的,'add_fragment' =>'');$paginate_links = paginate_links($pagination_args);如果($ paginate_links){echo "<nav class='custom-pagination'>>>echo "<span class='page-numbers page-num'>Page ".$分页."的".$numpages .</span>";回声 $paginate_links;echo "</nav>";}}

Good day! The problem is this: in the template category(the archive) the pagination is not working, when you click on page 2 of the 404 error. Please help do not understand how to solve it, already all head broke

My loop:

<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$arg = array(
  'cat' => get_queried_object_id(),
  'post_type'=>'post',
  'posts_per_page'=>9,
  //'order'=>'desc',
  'paged' => $paged,
  );
$query = new WP_query($arg);
if($query->have_posts()) : ?>
<section class="blog">
  <?php
  echo '<div class="row">';
  $i=0;
  $formcreated=false;
  while( $query->have_posts() ) :
    $query->the_post();
    // display post 
   endwhile;
   wp_reset_postdata();
endif;
?>

<div class="pagination">
  <?php
       if (function_exists('custom_pagination')) {
         custom_pagination($query->max_num_pages,"",$paged);
       }
     ?>
   <?php wp_reset_postdata(); ?>
</div>

And my custom pagination:

function my_post_queries( $query ) {
    // do not alter the query on wp-admin pages and only alter it if it's the main query
    if (!is_admin() && $query->is_main_query()){

        // alter the query for the home and category pages


        if(is_category()){
            $query->set('posts_per_page', 1);
            $query->set('post_type','product');
        }

    }
}
add_action( 'pre_get_posts', 'my_post_queries' );

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'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&#60;'),
    'next_text'       => __('&#62;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo $paginate_links;
    echo "</nav>";
  }

}

解决方案

Since this has come up in two different forums lately, I am answering this.

If you use custom pagination, such as the one you're using which appears to come from http://callmenick.com/post/custom-wordpress-loop-with-pagination but it happens with Genesis child themes too because the parent numbered pagination is what people what.

Why do you get a 404 page? The Custom pagination in the callmenick.com and Genesis (genesis_posts_nav) is for the main query and so if your pagination for your different query is under the posts per page in your reading settings (which is set for the main query), then you will get a 404 on page 2.

Every front end page request on a WordPress site produces a main query. The template that WordPress decides to load is based on the results of that main query (you can see the order that WordPress does these things by looking at the Action Reference page). Despite the fact that you never output the results of that query, it's still run, and in the case of paginated archives, this is an issue if you're trying to use that pagination for a different query. — Milo https://wordpress.stackexchange.com/a/120963/64742

You don't see this question a lot because many just build the pagination for that loop instead of re-using it from the functions.php file or a parent theme. You can learn that here: https://codex.wordpress.org/Function_Reference/paginate_links

Let's start from the top and whenever you code, turn on debug in wp-config.php


A basic custom loop in my cpt archive.

archive-product.php

 <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

  $product_args = array(
      'post_type' => 'product',
      'posts_per_page' => 2, //the same as the parse_query filter in our functions.php file
      'paged' => $paged,
      'page' => $paged
    );

  $product_query = new WP_Query( $product_args ); ?>

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

    <!-- the loop -->
    <?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
      <article class="loop">
        <h3><?php the_title(); ?></h3>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- end of the loop -->


    <!-- pagination here -->
    <?php
       if (function_exists( 'custom_pagination' )) :
          custom_pagination( $product_query->max_num_pages,"",$paged );
      endif;
   ?>


  <?php wp_reset_postdata(); ?>

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


In your functions.php file:

Learn about conditionals. https://codex.wordpress.org/Conditional_Tags https://codex.wordpress.org/Function_Reference/is_post_type_archive

/** 
 * Posts per page for CPT archive
 * prevent 404 if posts per page on main query
 * is greater than the posts per page for product cpt archive
 *
 * thanks to https://sridharkatakam.com/ for improved solution!
 */

function prefix_change_cpt_archive_per_page( $query ) {
    
    //* for cpt or any post type main archive
    if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
        $query->set( 'posts_per_page', '2' );
    }

}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );

/**
 * 
 * Posts per page for category (test-category) under CPT archive 
 *
*/
function prefix_change_category_cpt_posts_per_page( $query ) {

    if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
        $query->set( 'post_type', array( 'product' ) );
        $query->set( 'posts_per_page', '2' );
    }

}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );


/**
*
* custom numbered pagination 
* @http://callmenick.com/post/custom-wordpress-loop-with-pagination
* 
*/
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'          => '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>";
  }

}

相关文章