带有分页功能的WordPress自定义帖子类型类别视图

我有一个自定义帖子类型” charters” , 在我的category.php页面中, 我有一个循环, 用于获取当前类别ID的” charter” 帖子。我有一个自定义分页设置, 当我单击第2页链接时显示页码, 它转到URL / page / 2, 它显示404错误。我不知道为什么会得到404。如果我每页调整我的帖子, 则分页会更新并显示正确的页面数, 但链接不会显示。
Category.php代码:

< ?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $product_args = array( 'post_type' => 'charters', 'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file 'paged' => $paged, 'page' => $paged, 'cat' => $cat ); $product_query = new WP_Query( $product_args ); ?> < ?php if ( $product_query-> have_posts() ) : ?> < !-- the loop --> < ?php while ( $product_query-> have_posts() ) : $product_query-> the_post(); ?> < article class="loop"> < h3> < ?php the_title(); ?> < /h3> < div class="content"> < ?php the_excerpt(); ?> < /div> < /article> < ?php endwhile; ?> < !-- end of the loop --> < !-- pagination here --> < ?php if (function_exists( 'custom_pagination' )) : custom_pagination( $product_query-> max_num_pages, "", $paged ); endif; ?> < ?php wp_reset_postdata(); ?> < ?php else:?> < p> < ?php _e( 'Sorry, no posts matched your criteria.' ); ?> < /p> < ?php endif; ?>

Functions.php代码:
function prefix_change_cpt_archive_per_page( $query ) {//* for cpt or any post type main archive if ( $query-> is_main_query() & & ! is_admin() & & is_post_type_archive( 'product' ) ) { $query-> set( 'posts_per_page', '10' ); }} add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' ); function prefix_change_category_cpt_posts_per_page( $query ) {if ( $query-> is_main_query() & & ! is_admin() & & is_category( 'test-category' ) ) { $query-> set( 'post_type', array( 'product' ) ); $query-> set( 'posts_per_page', '2' ); }} add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' ); function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {if (empty($pagerange)) { $pagerange = 2; }global $paged; if (empty($paged)) { $paged = 1; } if ($numpages == '') { global $wp_query; $numpages = $wp_query-> max_num_pages; if(!$numpages) { $numpages = 1; } }$pagination_args = array( 'base'=> get_pagenum_link(1) . '%_%', 'format'=> 'page/%#%', 'total'=> $numpages, 'current'=> $paged, 'show_all'=> False, 'end_size'=> 1, 'mid_size'=> $pagerange, 'prev_next'=> True, 'prev_text'=> __('& laquo; '), 'next_text'=> __('& raquo; '), 'type'=> 'plain', 'add_args'=> false, 'add_fragment'=> '' ); $paginate_links = paginate_links($pagination_args); if ($paginate_links) { echo "< nav class='custom-pagination'> "; echo "< span class='page-numbers page-num'> Page " . $paged . " of " . $numpages . "< /span> "; echo $paginate_links; echo "< /nav> "; }

我的永久链接设置设置为” 帖子名称” , 我感觉这与重写有关, 但是我不知道如何使页面URL正常工作。我的类别的示例如下:
/类别/长滩/
/ category / san-diego /
#1你应该尝试这样:
< ?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'charters', 'cat'=> $cat, 'post_per_page'=> 6, 'paged' => $paged ); $my_query = new WP_Query( $args ); if( $my_query-> have_posts() ) { while ($my_query-> have_posts()) : $my_query-> the_post(); ?> < p> < a href="http://www.srcmini.com/< ?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title_attribute(); ?> "> < ?php the_title(); ?> < /a> < /p> < ?php endwhile; }wp_reset_query(); ?> < ?php echo pnavigation( $query ); ?>

那么你需要向function.php添加以下代码:
function pnavigation( $wp_query ) {$big = 999999999; // need an unlikely integer $pages = paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query-> max_num_pages, 'prev_next' => false, 'type'=> 'array', 'prev_next'=> TRUE, 'prev_text'=> '& larr; ', 'next_text'=> '& rarr; ', ) ); if( is_array( $pages ) ) { $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged'); echo '< ul class="pagination pagination-lg"> '; foreach ( $pages as $page ) { echo "< li> $page< /li> "; } echo '< /ul> '; } }

你只需要用url替换url(客户推荐)
function pagination_rewrite() { add_rewrite_rule('client-testimonials/page/?([0-9]{1, })/?$', 'index.php?pagename=client-testimonials& paged=$matches[1]', 'top'); } add_action('init', 'pagination_rewrite');

#2如果你使用默认分页, 请使用以下代码。
< ?php $current_page = get_queried_object(); $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => 'charters', 'paged'=> $paged, 'cat'=> $cat); $my_query = new WP_Query( $args ); if( $my_query-> have_posts() ) { while ($my_query-> have_posts()) : $my_query-> the_post(); ?> < p> < a href="http://www.srcmini.com/< ?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title_attribute(); ?> "> < ?php the_title(); ?> < /a> < /p> < ?php endwhile; } next_posts_link( 'Older Entries', $my_query-> max_num_pages ); previous_posts_link( 'Newer Entries' ); wp_reset_query(); ?>

在你的function.php中添加以下代码
function posts_on_categorypage( $query ) { if ( $query-> is_category()) { $query-> set( 'posts_per_page', '10' ); } } add_action( 'pre_get_posts', 'posts_on_categorypage' );

#3我的代码中有几个错误。这是我获得的复制/粘贴代码, 而不是逐行通过代码。感谢所有提供帮助的人, 但我通过更改以下内容使它起作用:
【带有分页功能的WordPress自定义帖子类型类别视图】错误代码:
function prefix_change_category_cpt_posts_per_page( $query ) {if ( $query-> is_main_query() & & ! is_admin() & & is_category( 'test-category' ) ) { $query-> set( 'post_type', array( 'product' ) ); $query-> set( 'posts_per_page', '2' ); }}

好代码:
function prefix_change_category_cpt_posts_per_page( $query ) {if ( $query-> is_main_query() & & ! is_admin() & & is_category( $cat ) ) { $query-> set( 'post_type', array( 'charters' ) ); $query-> set( 'posts_per_page', '10' ); }}

    推荐阅读