WordPress:Function Reference/wp count posts

来自站长百科
Fludlen讨论 | 贡献2008年7月19日 (六) 15:34的版本
跳转至: 导航、​ 搜索

Description

描述

This function was introduced in WordPress WordPress:Version 2.5, and outputs the number for each post status of a post type. You can also use wp_count_posts() as a template_tag with the second parameter and include the private post status. By default, or if the user isn't logged in or is a guest of your site, then private post status post count will not be included.

这是函数是在WordPress 2.5版本中引进的,而且输出某种文章类型的不同级别的文章的字数。你也可以用第二个参数,将wp_count_posts()用作template_tag,并且包含文章保密级别。默认情况下,如果用户登录进来,或者用户是你的站点的访客,那么就不会看到保密的文章的字数。

This function will return an object with the post statuses as the properties. You should check for the property using isset() PHP function, if you are wanting the value for the private post status. Not all post statuses will be part of the object.

这个函数会返回文章级别作为属性的object。如果你想知道保密的文章的字数,你应该使用isset() PHP函数,查看属性。并不是所有的文章级别都是object的一部分。

Usage

用法

%%% <?php wp_count_posts('type'); ?> %%%


%%% <?php wp_count_posts('type'); ?> %%%


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

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

Examples

Default Usage

例子

默认用法

The default usage returns a count of the posts that are published. This will be an object, you can var_dump() the contents to debug the output. 默认用法,返回已经发表的文章的字数,这是object,你可以var_dump()内容,调试输出结果。

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

Get the Publish Status Post Count

得到已经发表的文章的字数

To get the published status type, you would call the wp_count_posts() function and then access the 'publish' property.

要得到已经发表的文章,你应该调用wp_count_posts()函数,然后访问'发表'属性。

<?php
$count_posts = wp_count_posts();

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

$published_posts = $count_posts->publish;
?>

If you are developing for PHP5 only, then you can use shorthand, if you only want to get one status. This will not work in PHP4 and if you want to maintain backwards compatibility, then you must use the above code.

如果你只想得到一种级别的文章,如果你只是发展了PHP5,你可以使用速记。速记不能够在PHP4中运行,如果你想要维持向后兼容性,你必须使用以上的代码。

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

Count Drafts

草稿的字数

Counting drafts is handled the same way as the publish status.

草稿字数的计算方法与已经发表的文章的计算方法是相同的。

<?php
$count_posts = wp_count_posts();

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

$draft_posts = $count_posts->draft;
?>

Count Pages

计算网页字数

Counting pages status types are done in the same way as posts and make use of the first parameter. Finding the number of posts for the post status is done the same way as for posts.

计算网页的字数的方式与计算文章的字数方式是相同的,而且都是利用了第一个参数。计算出某个级别的文章有多少篇的方式与计算文章中有多少字数是相同的。

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

Other Uses

其它的用法

The wp_count_posts() can be used to find the number for post statuses of any post type. This includes attachments or any post type added in the future, either by a plugin or part of the WordPress Core.

wp_count_posts()可以用来,找出任何类型的文章的文章不同级别的字数,包括附属,或者将来通过插件或者WordPress部分核心添加的任何文章类型的文章的字数,

Parameters

参数

type
(string) Type of row in wp_posts to count where type is equal to post_type. Defaults to post
type
(string) wp_posts中的row的type,计算出type在哪里等同于post_type。默认为文章
perm
(string) To include private post status, use 'readable' and requires that the user be logged in. Default to empty string
perm
(string)使用'可读的',同时用户已经登录了,就可以阅读保密的文章。默认为空字符串