WordPress post 号

Wordpress post number

是否可以显示 post 的编号(不是 ID),就像 post 列表中的顺序一样?

例如:我有 30 个 post。最新的是1号,最后的是30号。

对数据库进行查询,以帖子 table 为目标,选择 post_type = 'post'(或 'page',无论您要枚举哪个),以及 post_status = 'publish'(否则您还将列举草稿)。如果您选择得到 array_N 作为结果,您可以使用数组键来创建您正在寻找的 "post numbers"。 这是该查询的示例:

global $wpdb; // call the global database object
$html = '';
/* get the title of each post with status 'publish' */
$serial_num_post = $wpdb->get_results( 
    "SELECT post_title 
    FROM $wpdb->posts 
    WHERE post_type = 'post' 
    AND post_status = 'publish'", 
    ARRAY_N );
if ($serial_num_post!=NULL) { // are there any posts?
     foreach ($serial_num_post as $key => $value){
         $html .= 'post# '.($key+1).' - '.$value[0].'<br />';
     }
}
echo 'Total: '.count($serial_num_post).' posts<br />';
echo $html;