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>


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


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



慕盖茨4494581
浏览 134回答 4
4回答

梵蒂冈之花

试试下面(替换你的html)<?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $loop;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = $query ? $query : $loop;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $big = 999999999;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paginate = paginate_links( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type' => 'array',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'total' => $query->max_num_pages,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'format' => '?paged=%#%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'current' => max( 1, get_query_var('paged') ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'prev_previous'&nbsp; =>('&larr; Older posts'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'prev_next'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => ( 'Newer posts &rarr;' ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'prev_text' => __('&laquo;'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'next_text' => __('&raquo;'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($query->max_num_pages > 1) :&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="paginate">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="pagination">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $paginate as $page ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<li>' . $page . '</li>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php endif; ?>

慕桂英546537

我最近遇到了同样的问题。不知道是什么问题,但这对我有用。$pagination = [&nbsp; &nbsp; 'base' => @add_query_arg('paged','%#%'),&nbsp; &nbsp; 'format' => '',&nbsp; &nbsp; 'total' => $the_query->max_num_pages,&nbsp; &nbsp; 'current' => get_query_var('paged') ?? 1,&nbsp; &nbsp; 'show_all' => false,&nbsp; &nbsp; 'mid_size' => 4,&nbsp; &nbsp; 'type' => 'list',&nbsp; &nbsp; 'next_text' => '&raquo;',&nbsp; &nbsp; 'prev_text' => '&laquo;'];echo paginate_links($pagination);

缥缈止盈

我遇到了同样的问题,只是发现我的问题的原因是我为我的自定义帖子类型和显示新 WP_Query 的页面设置了相同的 slug。

杨魅力

代替$big = 999999999;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo paginate_links( array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'format' => '?paged=%#%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'current' => max( 1, get_query_var('paged') ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'total' => $loop->max_num_pages&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;) );和&nbsp; global $loop;&nbsp; $query = $query ? $query : $loop;&nbsp; $big = 999999999;&nbsp; $paginate = paginate_links( array(&nbsp; &nbsp; 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),&nbsp; &nbsp; 'type' => 'array',&nbsp; &nbsp; 'total' => $query->max_num_pages,&nbsp; &nbsp; 'format' => '?paged=%#%',&nbsp; &nbsp; 'current' => max( 1, get_query_var('paged') ),&nbsp; &nbsp; 'prev_previous'&nbsp; =>('&larr; Older posts'),&nbsp; &nbsp; 'prev_next'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => ( 'Newer posts &rarr;' ),&nbsp; &nbsp; 'prev_text' => __('&laquo;'),&nbsp; &nbsp; 'next_text' => __('&raquo;'),&nbsp; ));&nbsp; if ($query->max_num_pages > 1) :如果不起作用,我有另一个解决方案,请问我
打开App,查看更多内容
随时随地看视频慕课网APP