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

954次阅读
没有评论

共计 728 个字符,预计需要花费 2 分钟才能阅读完成。

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

<?php wp_count_posts('type', 'readable'); ?>

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

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

wp_count_posts()的返回值为数组

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、获取已发布文章的数量、

<?php 
	$count = wp_count_posts();
	$getCount = $count->publish;
	echo $getCount;
?>

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

<?php 
	$count = wp_count_posts('page');
	$getCount = $count->publish;
	echo $getCount;
?>
正文完
 0
评论(没有评论)