为什么分页不工作,并给出了一个404错误的WordPress网站?

rvpgvaaj  于 2023-06-21  发布在  WordPress
关注(0)|答案(3)|浏览(136)

日安!问题是:在模板类别(存档)分页不工作,当你点击页面2的404错误.不懂的请帮忙解决吧,已经都头破了

我的循环:

<?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>

我的自定义分页:

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>";
  }

}
ou6hu8tu

ou6hu8tu1#

由于最近在两个不同的论坛上出现了这个问题,我回答这个问题。
如果您使用自定义分页,例如您正在使用的似乎来自http://callmenick.com/post/custom-wordpress-loop-with-pagination的分页,但它也会发生在Genesis子主题中,因为父编号分页是人们所使用的。
为什么会出现404页面?callmenick.com和Genesis(genesis_posts_nav)中的自定义分页是针对main查询的,因此如果您的不同查询的分页在您的阅读设置(为主查询设置)中的每页文章下,那么您将在第2页获得404。
WordPress网站上的每个前端页面请求都会产生一个主查询。WordPress决定加载的模板是基于主查询的结果(您可以通过查看Action Reference页面来查看WordPress执行这些操作的顺序)。尽管您从未输出该查询的结果,但它仍然在运行,并且在分页归档的情况下,如果您试图将该分页用于不同的查询,则会出现问题。
-米洛https://wordpress.stackexchange.com/a/120963/64742**
你不会经常看到这个问题,因为很多人只是为那个循环构建分页,而不是从functions.php文件或父主题中重用它。你可以在这里学习:https://codex.wordpress.org/Function_Reference/paginate_links

  • 让我们从头开始,无论何时编写代码,都要在wp-config.php中打开debug *
    我的cpt归档中的一个基本自定义循环。

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; ?>

在functions.php文件中:

学习条件句。https://codex.wordpress.org/Conditional_Tagshttps://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>";
  }

}
bvk5enib

bvk5enib2#

在wp-include/functions.php上
添加这些行

function my_pagination_rewrite() {
 add_rewrite_rule('([a-z]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]', 'top');
}

add_action('init', 'my_pagination_rewrite');
c90pui9n

c90pui9n3#

我在我的网站https://chronodivers.com上也遇到了同样的问题,我有3个类似的网站都运行相同的主题,插件,WP版本(5.6)等。只有这个网站有问题。
所有网站都使用PERMALINK格式/%category%/%postname%/
我试过插件(https://wordpress.org/support/view/plugin-reviews/category-pagination-fix)不适合我。
然后我执行了这些简单的前端任务
1.将Permalink格式更改为PLAIN并保存。
1.刷新缓存
1.将Permalink格式更改回/%category%/%postname%/并保存40刷新缓存
1.我得到了一个“警告”弹出从YOAST插件建议,由于永久链接结构的变化,它可能需要重建 IndexedDB (不记得确切的措辞),它提到了WP CLI...
1.我只是去了YOAST >工具>优化SEO数据

  1. 5完成后清除缓存
    1.它的工作。我已经检查了所有类别,他们都工作
    没有改变PHP文件,htaccess等.这次是为了我

相关问题