简体   繁体   中英

Wordpress custom post type archive pagination results in 404 error

Whenever I try to access another page using the archive's pagination I get a 404 error. Apparently a well known problem but so far no solution has worked in my case. I might have been applying it wrong though.

Following advice I found online and in the codex I currently have the archive page template set up for a custom post-type like so:

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$args = array(
    'post_type' => 'application',
    'posts_per_page' => 2,
    'paged' => $paged
);

$query = new WP_Query($args);

if ($query->have_posts()) { ?>

    <div class="posts">
        <!-- THE POSTS -->
    </div>

    <?php
    $big = 999999999; // unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'prev_next'    => true,
        'prev_text'    => sprintf( '<i></i> %1$s', __( 'Previous', 'textdomain' ) ),
        'next_text'    => sprintf( '%1$s <i></i>', __( 'Next', 'textdomain' ) ),
        'current' => max( 1, get_query_var('paged') ),
        'total' => $query->max_num_pages
    ) );

    wp_reset_postdata();

} ?>

and have my permalinks set to /%category%/%postname%/ . I understand this could be part of the problem.

And this is where I kind of get lost. What am I doing wrong? Any help would be very much appreciated.

I would change the first line to:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1 ?>

Also adding this line after that first line might help:

$number_of_posts = get_option('posts_per_page', 2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM