ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-11 03:31:34  阅读:285  来源: 互联网

标签:php wordpress pagination wordpress-theming custom-wordpress-pages


美好的一天!问题是这样的:当您单击404错误的第2页时,在模板类别(档案库)中,分页不起作用.
请帮忙不明白该怎么解决,已经全死了

我的循环:

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

}

解决方法:

由于最近在两个不同的论坛中都提到了这一点,所以我在回答这个问题.

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

为什么会显示404页面?在callmenick.com和Genesis(genesis_posts_nav)中的“自定义”分页用于主要查询,因此,如果您对其他查询的分页在您的阅读设置(为主要查询设置)中每页的帖子下,在第2页上获取404.

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 07001

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

让我们从顶部开始,每当您编写代码时,在wp-config.php中打开调试功能

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

归档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_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>";
  }

}

标签:php,wordpress,pagination,wordpress-theming,custom-wordpress-pages
来源: https://codeday.me/bug/20191011/1889860.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有