我正在与CPT讨论一个主题。 在single-cpt.php中,我要显示总帖子中的帖子编号。
我设法使用:来显示所有好的帖子。
<?php $args = array(
'post_type' => 'cpt_type',
'post_status' => 'published',
'numberposts' => -1
);
echo $num = count( get_posts( $args ) ); ?>
,它返回该CPT类型的帖子总数。因此,在每一个cpt上,我得到了总数。
我想要的是拥有“职位编号” /“总职位”。例如:7/21,然后我去以下帖子:8/21,依此类推...
那么,我如何让每个帖子都有一个“数字”?
谢谢!
答案 0 :(得分:0)
将其粘贴在functions.php中:
function get_total_number_of_posts( $post ) {
$posts = get_posts( array(
'post_type' => $post->post_type,
'posts_per_page' => -1
) );
return count( $posts );
}
function get_current_post_index( $post ) {
$posts = get_posts( array(
'post_type' => $post->post_type,
'posts_per_page' => -1
) );
$index = 0;
foreach ( $posts as $p ) {
$index++;
if ( $p->ID === $post->ID ) {
break;
}
}
return $index;
}
,然后在您的single.php文件中:
global $post;
$total_posts = get_total_number_of_posts( $post );
$current_post = get_current_post_index( $post );
echo "You are viewing {$current_post} out of {$total_posts}";