Home  >  Article  >  php教程  >  在WordPress中使用wp_count_posts函数来统计文章数量,wordpresswpposts

在WordPress中使用wp_count_posts函数来统计文章数量,wordpresswpposts

WBOY
WBOYOriginal
2016-06-13 08:48:21972browse

在WordPress中使用wp_count_posts函数来统计文章数量,wordpresswpposts

做一个全站统计是不是很酷?长久的博客越来越少,何不给自己的一个统计,看看自己在这个博客上努力了多少,不但给自己也给游客,wp_count_posts是在 WordPress 中用来统计文章数量的函数,可以统计所有类型的文章(post)和页面(page)。

描述
wp_count_posts是在 WordPress 中用来统计文章数量的函数,可以统计所有类型的文章(post)和页面(page)。

使用

//获取文章数量
$postcount = wp_count_posts();
//获取页面数量
$pagecount = wp_count_posts('page');

返回值
使用上大致也就上面两种形式,
该函数的使用主要是在返回值上,
上面两种形式都会返回一个对象,
如下:可能数值上不太一样,但结构是一样的

object(stdClass)#296 (8) {
//已发布
 ["publish"]=>string(1) "7"
//定时发布
 ["future"]=>int(0)
//草稿
 ["draft"]=>int(0)
//正在编辑的?(有待考证)
 ["pending"]=>int(0)
//隐私
 ["private"]=>int(0)
//垃圾箱
 ["trash"]=>int(0)
//自动草稿
 ["auto-draft"]=>int(0)
//这个不知道,也没研究
 ["inherit"]=>int(0)
}

缺省用法

缺省用法返回已发表文章的计数。这是一个对象,可以在内容中用var_dump()来调试输出结果。

<?php 
$count_posts = wp_count_posts(); 
?>

获取发表状态文章的计数

要获取已发表文章的状态类型,应先调用wp_count_posts()函数然后确查看“发表”属性。

<?php 
$count_posts = wp_count_posts();  
$published_posts = $count_posts->publish; 
?>

若使用PHP5且只想获取一个文章状态,可以用下面更为便捷的方法。这个代码在PHP4中无法运行,因此如果希望保持后台的兼容性,应该选择上面的代码。

<?php 
$published_posts = wp_count_posts()->publish; 
?>

草稿计数

计算草稿数量方法与获取发表状态计数方法一致。

<?php 
$count_posts = wp_count_posts();  
$draft_posts = $count_posts->draft; 
?>

页面计数

计算页面类型数量的方法与计算文章一样,也需要利用第一个参数。查找某一状态下的页面数量与查找该状态下的日志方法相同。

<?php 
$count_pages = wp_count_posts('page'); 
?>

其他用法

wp_count_posts() 可查找日志状态下任何日志类型的数量,包括附件以及将要添加的任何日志类型,也可以通过插件或WordPress部分核心团队达到这一效果。

参数

type

(字符)wp_posts中行的类型,用于核查哪种类型与post_type对应。默认post。

perm

(字符)该参数可将私密文章状态算入文章状态中,使用'readable'并要求用户登录。默认为empty string。

您可能感兴趣的文章:

  • WordPress开发中用于标题显示的相关函数使用解析
  • WordPress中调试缩略图的相关PHP函数使用解析
  • 配置解决Nginx服务器中WordPress路径不自动加斜杠问题
  • WordPress中用于获取搜索表单的PHP函数使用解析
  • 详解WordPress中调用评论模板和循环输出评论的PHP函数
  • 详解WordPress中分类函数wp_list_categories的使用
  • WordPress中限制非管理员用户在文章后只能评论一次
  • 详解WordPress中创建和添加过滤器的相关PHP函数
  • 详解WordPress开发中wp_title()函数的用法
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn