我有自定义的帖子类型称为客户端,需要每页显示5个客户端并具有分页功能。我所拥有的页面是page-clients.php
我使用了wp_pagenavi插件。
我得到了一个完美的导航列表1,2,3等等,但点击它们会带我到找不到的页面
我的代码
$args = array(
'posts_per_page' => 5,
'post_type' => 'clients',
'paged' => get_query_var('page')
);
query_posts($args);
<?php while ( have_posts() ) : the_post(); ?>
.....
<?php endwhile; // end of the loop. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query();?>
答案 0 :(得分:5)
使用WP_QUERY
代替query_posts
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'clients',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
答案 1 :(得分:3)
分页赞:上一页1 2 3下一页
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>’YOUR_POST_TYPE’, // your post type name
'posts_per_page' => 5, // post per page
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your code
endwhile;
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
请您尝试上面的代码吗?