帖子ID并不意味着是连续的。要按顺序对帖子编号,必须使用元字段。回到这个问题上,有人对此进行了讨论:
Change Permalinks Structure to a Sequential Number for Each Post?
对于您想要做的事情,最好的答案似乎是发布在支持论坛上的一段代码:
http://wordpress.org/support/topic/display-sequential-post-numbercount-not-post-id
function updateNumbers() {
/* numbering the published posts, starting with 1 for oldest;
/ creates and updates custom field \'incr_number\';
/ to show in post (within the loop) use <?php echo get_post_meta($post->ID,\'incr_number\',true); ?>
/ alchymyth 2010 */
global $wpdb;
$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts
WHERE $wpdb->posts.post_status = \'publish\'
AND $wpdb->posts.post_type = \'post\'
ORDER BY $wpdb->posts.post_date ASC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 0 ;
if ($pageposts):
foreach ($pageposts as $post):
$counts++;
add_post_meta($post->ID, \'incr_number\', $counts, true);
update_post_meta($post->ID, \'incr_number\', $counts);
endforeach;
endif;
}
add_action ( \'publish_post\', \'updateNumbers\', 11 );
add_action ( \'deleted_post\', \'updateNumbers\' );
add_action ( \'edit_post\', \'updateNumbers\' );
(我对其进行了非常轻微的修改,以按发布日期排序,而不是像最初的代码那样仅按ID排序。)
它将连接到所有帖子操作(保存/发布/删除),并更新所有帖子的“incr\\u number”自定义字段。因此,当数据库中有大量帖子时,保存帖子时会出现性能滞后,但一旦帖子被保存,显示数字就非常简单
echo get_post_meta( $post->ID, \'incr_number\', true );