How to Display Post Serial Number within a Category in WordPress

Display Post Serial Number within a Category in WordPress

Let's find out how to display the post serial number within a category in WordPress as shown above. The basic idea is to display the following:

Post Serial Number + Number of Posts per Page*(Page Number - 1)

Based on the above formula, the following code can be used to display the post serial number within a category:

<?php
$paged = get_query_var( 'paged' );
if($paged==0) {
$paged = 1;
}

$post_count = $wp_query->current_post +1 + $posts_per_page*($paged - 1);
if($post_count<10){
echo '0' . $post_count;
}
else {
echo($post_count);
}
?>

The code is somewhat complex. The page number for the first page is not 1 but 0. So it's modified to output "1" for the first page number. And I added a code to add "0" for the post number which is less than ten to make it in the format such as 01, 02... The post serial number is displayed without problems for the second page, the third page...

The list of posts when the number of posts per page is set to 25
The list of posts when the number of posts per page is set to 25

Leave a Comment

3s