首页 > 解决方案 > Wordpress 自定义帖子类型分页 - /page/2/ 未找到

问题描述

这真让我抓狂。我已经尝试了所有我能找到的解决方案,但仍然不适合我。

我有一个自定义帖子类型。

/**
 * Videos Custom Post Type
 */
function videos_post_type() {
    register_post_type( 'videos', array(
        'labels' => array(
            'name' => 'Videos',
            'singular_name' => 'Video',
            'add_new_item' => 'Add New Video',
            'add_new' => 'Add New Video',
            'edit_item' => 'Edit Video',
            'new_item' => 'New Video',
            'all_items' => 'All Videos'
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'videos' ),
        'menu_icon' => 'dashicons-format-video'
    ));
}
add_action( 'init', 'videos_post_type' );

除了“存档”页面上的分页外,一切正常。分页链接正确显示,但是当我单击“下一步”时,它以找不到页面错误结束。

<div class="ast-row">
        <?php

            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $loop = new WP_Query( array(
            'post_type' => 'videos',
            'posts_per_page' => 1,
            'orderby'=> 'menu_order',
            'paged'=> $paged
            ) );

            while ( $loop->have_posts() ) {
                $loop->the_post(); ?>
                <div class="ast-col-md-4">
                    <?php echo the_content(); ?>
                </div>
            <?php } ?>

        </div>
        <div class="ast-row">
            <?php
                echo paginate_links( array(
                   'total' => $loop->max_num_pages
               ) );
            ?>
        <?php  wp_reset_postdata(); ?>
        </div>

还尝试了以下 echo_paginate_links()

                $big = 999999999; 
                echo paginate_links( array(
                   'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                   'format' => '?paged=%#%',
                   'current' => max( 1, get_query_var('paged') ),
                   'total' => $loop->max_num_pages
               ) );

此外- * 我试图保存永久链接 * 我试图更改永久链接结构 * 我尝试过 query_posts 而不是 WP_Query ---这些都不起作用。

我还应该提到我还没有创建一个 single-videos.php 模板(因为我不需要它来处理这种帖子类型),但我认为这与这个问题没有任何关系。如果我错了,请纠正我。我只是无法理解它。如果有帮助,我正在使用 Astra 主题。

请分享您的建议。谢谢!

标签: phpwordpresswordpress-themingcustom-post-typecustom-wordpress-pages

解决方案


试试下面(替换你的html)

<?php
          global $loop;
          $query = $query ? $query : $loop;
          $big = 999999999;

          $paginate = paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'type' => 'array',
            'total' => $query->max_num_pages,
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'prev_previous'  =>('&larr; Older posts'),
            'prev_next'          => ( 'Newer posts &rarr;' ),
            'prev_text' => __('&laquo;'),
            'next_text' => __('&raquo;'),
          ));
          if ($query->max_num_pages > 1) :
        ?>

        <div class="paginate">
          <ul class="pagination">
            <?php
              foreach ( $paginate as $page ) {
                echo '<li>' . $page . '</li>';
              }
            ?>
          </ul>
        </div>
         <?php endif; ?>

推荐阅读