如何获取当前帖子的序列号?
对于前。我有一个类别的汽车有4个帖子。当我打开一些帖子时,我希望看到这样的导航:帖子3中的4 [<<<<<>>>]
答案 0 :(得分:1)
最直接的方法是查询类别中的帖子,如下所示:
// WP_Query arguments
$args = array (
'category_name' => 'cars',
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
然后你可以用
获得帖子数量// $query->found_posts gives the number of posts the query has found
// with the parameters you set
echo $query->found_posts;
您可以对显示的帖子进行统计:
$count = 0;
foreach ( $query->posts as $count_post ) {
$count++;
// assuming you are inside The Loop
if ( get_the_ID() == $count_post->ID ) {
break;
}
}
// now you can get the "serial number" of the post
echo $count;
这可能不是最常用的“WP方式”,但它应该有效。 :)