我正在尝试将分页添加到archive-CustomPostType.php页面中的以下代码,但它不显示分页或显示空白页面。 我的代码:
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif;
?>
</div>
<?php get_footer(); ?>
我正在使用页面导航进行分页,但我不认为这是问题,因为我尝试使用正常的wordpress分页但没有帮助。
我认为问题来自get_queried_object
,我不知道如何处理这个问题。
任何帮助都将不胜感激。
答案 0 :(得分:0)
您可以尝试此代码。
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => 'Previous',
'next_text' => 'Next',
) );
?>
</div>
<?php else : ?>
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
?>
</div>
<?php get_footer(); ?>
修改functions.php中的查询帖子类型
add_action( 'pre_get_posts' ,'wpse222471_query_post_type_portofolio', 1, 1 );
function wpse222471_query_post_type_portofolio( $query )
{
if ( ! is_admin() && is_post_type_archive( 'customposttype' ) && $query->is_main_query() )
{
$query->set( 'posts_per_page', 6 ); //set query arg ( key, value )
}
}