Php 在wordpress中获取当前帖子编号(非ID)
我正在使用此函数获取帖子数量:Php 在wordpress中获取当前帖子编号(非ID),php,wordpress,Php,Wordpress,我正在使用此函数获取帖子数量: $postAmount = wp_count_posts( 'post' )->publish; 这将返回4,这是正确的数字。 但是,还有一个功能,我可以检查当前的职位号码吗?不是ID,而是一个数字 例如,我在第二篇文章中,所以我希望函数返回'2' 额外信息 $wp->query->current_post+1在每篇文章中返回0您的想法是正确的,但只是输入了一个小错误。你想要的是 $wp_query->current_post 或 取决于你是否想从
$postAmount = wp_count_posts( 'post' )->publish;
这将返回4,这是正确的数字。
但是,还有一个功能,我可以检查当前的职位号码吗?不是ID,而是一个数字
例如,我在第二篇文章中,所以我希望函数返回'2'
额外信息
$wp->query->current_post+1在每篇文章中返回0您的想法是正确的,但只是输入了一个小错误。你想要的是
$wp_query->current_post
或
取决于你是否想从零开始计数
在WP_查询法典中搜索当前的_帖子你的想法是对的,只是打了一个小错误。你想要的是
$wp_query->current_post
或
取决于你是否想从零开始计数
在WP\u查询法典中搜索当前帖子使用$WP\u Query->current\u post+1而不是$WP->Query->current\u post+1 完整代码,如
<?php
$postArg = array('post_type'=>'post',
'posts_per_page'=>-1,
'order'=>'desc',
);
global $post;
$getPost = new wp_query($postArg);
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
$index= $getPost->current_post + 1;
echo "<h2>".$post->post_title."</h2>".$index;
endwhile;
echo '</ul>';
}
?>
使用$wp->query->current\u post+1代替$wp->query->current\u post+1 完整代码,如
<?php
$postArg = array('post_type'=>'post',
'posts_per_page'=>-1,
'order'=>'desc',
);
global $post;
$getPost = new wp_query($postArg);
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
$index= $getPost->current_post + 1;
echo "<h2>".$post->post_title."</h2>".$index;
endwhile;
echo '</ul>';
}
?>
哦,哈哈,当然是打字错误哈哈。现在可以用了,谢谢!哦,哈哈,当然是打字错误哈哈。现在可以用了,谢谢!