最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Pagination in category.php not functioning

programmeradmin3浏览0评论

Original Url: /

When I add pagination in, the following url is generated which results in a 404:

Paginated Url: /

The code is located in category.php and looks like the following:

<?php
$category = get_category( get_query_var( 'cat' ) );
query_posts(array( 
'post_type' => 'video',
'showposts' => 6,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array($category->term_id)
)
)
));
?>
<?php while (have_posts()) : the_post(); ?> 

...

<?php endwhile;?>

paginate_links(array('total'=> $wp_query->max_num_pages));

I've tried various solutions to no avail, so I feel like something basic is being missed. Essentially, all I need to have happen is to restrict to 6 posts per page, and allow simple pagination.

Thank you so much!

Original Url: https://www.something/category/ftm-atf/

When I add pagination in, the following url is generated which results in a 404:

Paginated Url: https://www.something/category/ftm-atf/page/2/

The code is located in category.php and looks like the following:

<?php
$category = get_category( get_query_var( 'cat' ) );
query_posts(array( 
'post_type' => 'video',
'showposts' => 6,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array($category->term_id)
)
)
));
?>
<?php while (have_posts()) : the_post(); ?> 

...

<?php endwhile;?>

paginate_links(array('total'=> $wp_query->max_num_pages));

I've tried various solutions to no avail, so I feel like something basic is being missed. Essentially, all I need to have happen is to restrict to 6 posts per page, and allow simple pagination.

Thank you so much!

Share Improve this question asked Mar 11, 2020 at 19:06 Gene EllisGene Ellis 101 1
  • Never use query_posts(): rarst/wordpress/query-posts-breaks-pagination – Jacob Peattie Commented Mar 11, 2020 at 23:21
Add a comment  | 

1 Answer 1

Reset to default 1

if your aim is to restrict the category archive to your post_type 'video' and to 6 posts per page, do not edit category.php, rather use 'pre_get_posts' https://developer.wordpress/reference/hooks/pre_get_posts/

example code for your case:

add_action( 'pre_get_posts', 'category_post_type_video_custom' );
function category_post_type_video_custom( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        // Not a query for an admin page.
        // It's the main query for a front end page of your site.
        if ( is_category() ) {
            // It's the main query for a category archive.
            // Let's change the query for category archives.
            $query->set( 'post_type', array( 'video' ) );
            $query->set( 'posts_per_page', 6 );
        }
    }
}

virtually directly taken from https://developer.wordpress/reference/hooks/pre_get_posts/#targeting-the-right-query

发布评论

评论列表(0)

  1. 暂无评论