admin管理员组

文章数量:1387356

I'm trying to understand why my pagination gets 404 error on each page starts from second. Right now in Reading settings I've set Blog pages show at most to 1.

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = new WP_Query(
        array(
            'post_type'     => 'behold_testimonials',
            'orderby'       => 'rand',
            'paged'         => $paged
        )
    );

    if ($query->have_posts()):
        while ($query->have_posts()): $query->the_post();
            include( locate_template( '/elements/testimonials-thumbnail.php', false, false ) );
        endwhile;
    ?>

        <div class="l-pagination">

        <?php
            // kirki settings
            $pagination_layout = get_theme_mod( 'posts_general__pagination-style', true );
            if( 'layout-1' == $pagination_layout ) {
                $pagination_layout_style = 'pagination-1';
            } elseif( 'layout-2' == $pagination_layout ) {
                $pagination_layout_style = 'pagination-2';
            }
        ?>

            <div class="c-pagination <?php echo $pagination_layout_style; ?>">

            <?php
            $total_pages = $query->max_num_pages;
                echo paginate_links( array(
                    'base'          => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                    'format'        => '?paged=%#%',
                    'total'         => $total_pages,
                    'current'       => max(1, get_query_var('paged')),
                    'prev_text'     => __( '<i class="fas fa-caret-left"></i>', 'behold-standard' ),
                    'next_text'     => __( '<i class="fas fa-caret-right"></i>', 'behold-standard' ),
                ) );
            ?>

            </div>

        </div>

    <?php
        wp_reset_postdata();
    endif;

?>

Where is my mistake?

I'm trying to understand why my pagination gets 404 error on each page starts from second. Right now in Reading settings I've set Blog pages show at most to 1.

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = new WP_Query(
        array(
            'post_type'     => 'behold_testimonials',
            'orderby'       => 'rand',
            'paged'         => $paged
        )
    );

    if ($query->have_posts()):
        while ($query->have_posts()): $query->the_post();
            include( locate_template( '/elements/testimonials-thumbnail.php', false, false ) );
        endwhile;
    ?>

        <div class="l-pagination">

        <?php
            // kirki settings
            $pagination_layout = get_theme_mod( 'posts_general__pagination-style', true );
            if( 'layout-1' == $pagination_layout ) {
                $pagination_layout_style = 'pagination-1';
            } elseif( 'layout-2' == $pagination_layout ) {
                $pagination_layout_style = 'pagination-2';
            }
        ?>

            <div class="c-pagination <?php echo $pagination_layout_style; ?>">

            <?php
            $total_pages = $query->max_num_pages;
                echo paginate_links( array(
                    'base'          => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                    'format'        => '?paged=%#%',
                    'total'         => $total_pages,
                    'current'       => max(1, get_query_var('paged')),
                    'prev_text'     => __( '<i class="fas fa-caret-left"></i>', 'behold-standard' ),
                    'next_text'     => __( '<i class="fas fa-caret-right"></i>', 'behold-standard' ),
                ) );
            ?>

            </div>

        </div>

    <?php
        wp_reset_postdata();
    endif;

?>

Where is my mistake?

Share Improve this question asked Apr 27, 2020 at 11:11 D_PD_P 1531 gold badge3 silver badges12 bronze badges 3
  • 1 If you want to modify the main query you should use pre_get_posts to modify it, don't throw it away and replace it with a brand new custom query. You'll be getting a 404 because the main query doesn't have that page, but your new query does. Is this a page template? Or is it that this is a sub-section that you're trying to give its own independent pagination? Note that random ordering is extremely heavy on the database, it's one of the heaviest slowest things you can do in a post query. It's always better to simulate randomness, e.g. choose a random date then ask for the closest post – Tom J Nowell Commented Apr 27, 2020 at 11:21
  • This is section in page template that I wanna set to show all posts from my CPT. – D_P Commented Apr 27, 2020 at 11:57
  • Ok... thanks for paying attention for random query. Im gonna check how to do what you've mentioned :) – D_P Commented Apr 27, 2020 at 11:59
Add a comment  | 

1 Answer 1

Reset to default 0

The solution is that CPT should have 'has_archive' set to 'true'.

本文标签: Why my pagination gets 404