简体   繁体   中英

Custom post type archive pagination doesn't work on woo canvas child theme

I am in the process of building a child theme for woo canvas and I am struggling to get my pagination working on my archive page for a new custom post type called book.

The post type registeration code is as follows:

$args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'book' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

register_post_type( 'book', $args );

which is in my functions file and is working correctly.

I have the following code in my archive-book.php which is showing the pagination links but is giving me a 404 error whenever I navigate to a page that is paged eg - http://localhost/wp/book/page/2/ :

<?php

global $wp_query, $woo_options, $paged, $page, $post;
?>
<?php get_header(); ?>
<?php

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

$args = array(
    'post_type' => 'book',
    'paged' => $paged,
    'posts_per_page' => 3
    );
$query = new WP_Query( $args );
while ( $query->have_posts() ) { $query->the_post(); ?>
<?php echo the_title(); ?>
<?php } ?>
<?php 
woo_pagenav( $query );
wp_reset_query();
get_footer(); ?>

I have set the permalinks to 'Post name' and flushed the permalinks multiple times with both Post name and default permalinks selected.

The second page doesn't exist so your archive template is never loaded. The default number of posts per page will be used in the initial query and under that condition there is no second page of posts.

If you have 5 book posts for example under your custom query the first page would show 3 and the second 2. Under the default number of posts per page (10) all 5 would be shown on the first page and there would be no need for a page 2. WordPress then loads the 404 template.

You need to use the pre_get_posts hook to modify the main query.

Example:

/**
 * Change the number of posts per page on the book archive.
 *
 * @param object $query
 */
function wpse_modify_book_archive_query( $query ) {

    // Only apply to the main loop on the frontend.
    if ( is_admin() || ! $query->is_main_query() {
        return false;
    } 

    // Check we're viewing the book archives.
    if ( $query->is_post_type_archive( 'book' ) ) {
        $query->set( 'posts_per_page', 3 );
    }
}
add_action( 'pre_get_posts', 'wpse_modify_book_archive_query' );

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