wordpress文章数量统计函数wp_count_posts()

WordPress教程 1905

wp_count_posts()是用于统计指定文章类型文章数量的wordpress函数,通过wp_count_posts()函数可以统计所有类型的文章数量,如post、page或自定义文章类型post_type等,还可以计算指定状态的文章,如已发布、定时发布、草稿、待审、私有等。

代码结构:

1
<?php wp_count_posts('type', 'readable'); ?>
参数说明

type -(字符)可选,指定文章的类型(post_type),如post、page或其它自定义文章类型,默认为post。

perm -(字符)可选,该参数可将私密文章状态算入文章状态中,使用“readable”并要求用户登录,默认为空。

返回值

wp_count_posts()的返回值为数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
stdClass Object
(
    [publish] => 11	//已发布
    [future] => 0	//定时发布
    [draft] => 0	//草稿
    [pending] => 0	//待审
    [private] => 0	//私有
    [trash] => 0	//垃圾箱
    [auto-draft] => 34	//自动草稿
    [inherit] => 0	//修订版本
    [request-pending] => 0
    [request-confirmed] => 0
    [request-failed] => 0
    [request-completed] => 0
)

示例:

1、获取已发布文章的数量

1
2
3
4
5
<?php 
	$count = wp_count_posts();
	$getCount = $count->publish;
	echo $getCount;
?>

2、获取已发布页面的数量

1
2
3
4
5
<?php 
	$count = wp_count_posts('page');
	$getCount = $count->publish;
	echo $getCount;
?>

精品推荐: