下面的代码将获取所有类别的所有帖子。在这里,我们将一页限制为10页。然后你必须检查你网站的永久链接结构。请检查代码,我想它会工作的。
/* it will fetch all posts */
$post_args = array(
\'posts_per_page\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
);
$all_posts = get_posts( $post_args );
$post_count = count( $all_posts );
/* if post_count is greater than 0 */
if ( $post_count > 0 ) {
$limit = 10;
/* this is for getting the last post number */
if ( get_query_var( \'paged\' ) ) {
$current_page = get_query_var( \'paged\' );
} else {
$current_page = 1;
}
$permalink_structure = get_option( \'permalink_structure\' );
$format = empty( $permalink_structure ) ? \'&page=%#%\' : \'page/%#%/\';
$total_pages = ceil( $post_count / $limit );
$start = $current_page * $limit - $limit;
}
/* it will fetch posts under limit for all categories */
$post_args = array(
\'posts_per_page\' => $limit,
\'offset\' => $start,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
);
$current_posts = get_posts( $post_args );
?>
<!-- this is for showing pagination -->
<div class="tablenav">
<div class="tablenav-pages">
<?php
echo paginate_links(
array(
\'current\' => $current_page,
\'prev_text\' => \'« \' . __( \'Prev\' ),
\'next_text\' => __( \'Next\' ) . \' »\',
\'base\' => add_query_arg( \'paged\', \'%#%\' ),
\'format\' => $format,
\'total\' => $total_pages
)
);
?>
</div>
</div>
<!-- then here you can run the loop to show posts from $current_posts -->
<?php
/* if the posts are present */
if ( !empty( $current_posts ) ) {
foreach ( $current_posts as $post ) {
/* the code for showing posts */
}
}