[英]Wordpress - Pagination doesn't work with custom loop but working in blog pages
I have a loop with custom post types, and pagination doesn't appear, when I enter the URL with /page/2, /page/3... it shows the content correctly, but links don't appear on the page. 当我使用/ page / 2,/ page / 3输入URL时,我有一个自定义帖子类型的循环,并且没有出现分页...它正确显示了内容,但页面上没有显示链接。
Here is the code: 这是代码:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parent_only_query = new WP_Query(array(
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
//content
}
pagination(); ?>
Archive page with pagination working: 具有分页工作的存档页面:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?>
<?php endif; ?>
<?php pagination(); ?>
add this in your functions.php function pagination_nav() { global $wp_query; if ( $wp_query->max_num_pages > 1 ) { ?> <nav class="pagination" role="navigation"> <div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div> <div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div> </nav> <?php } } display on page.php <?php pagination_nav(); ?>
pagination can be shown in custom post type archive template and on custom template as well. 分页可以显示在自定义帖子类型归档模板和自定义模板中。
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
new WP_Query( $args );
// Call pagination function before wp_reset_postdata()
the_posts_pagination( array(
'prev_text' => '<span class="fa fa-angle-left" aria-hidden="true"></span>',
'next_text' => '<span class="fa fa-angle-right" aria-hidden="true"></span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
'mid_size' => 3,
) );
// Get current page.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array(
post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
$query = new WP_Query( $args );
$totalPage=$query->max_num_pages;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $totalPage
) );
You can check WordPress official document on Wordpress Codex 您可以在Wordpress Codex上查看WordPress官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.