WordPress自定义帖子类型分页-/ page / 2 /未找到

问题描述 投票:0回答: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主题。

请分享您的建议。谢谢!

php wordpress wordpress-theming custom-post-type custom-wordpress-pages
2个回答
0
投票

最近我遇到了同样的问题。不知道是什么问题,但这对我有用。

$pagination = [
    'base' => @add_query_arg('paged','%#%'),
    'format' => '',
    'total' => $the_query->max_num_pages,
    'current' => get_query_var('paged') ?? 1,
    'show_all' => false,
    'mid_size' => 4,
    'type' => 'list',
    'next_text' => '&raquo;',
    'prev_text' => '&laquo;'
];
echo paginate_links($pagination);

0
投票

替换

$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
           ) );

使用

  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) :

如果没有用,我还有另一个解决办法,问我吧>>

© www.soinside.com 2019 - 2024. All rights reserved.