WordPress: Function Reference/wp count posts:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
第1行: 第1行:
== 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 <code>wp_count_posts()</code> 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 [[WordPress:Version 2.5|2.5版本]]中引进的,而且输出某种文章类型的不同级别的文章的字数。你也可以用第二个参数,将<code>wp_count_posts()</code>用作template_tag,并且包含文章保密级别。默认情况下,如果用户登录进来,或者用户是你的站点的访客,那么就不会看到保密的文章的字数。
 
这是函数是在WordPress [[WordPress:Version 2.5|2.5版本]]中引进的,而且输出某种文章类型的不同级别的文章的字数。你也可以用第二个参数,将<code>wp_count_posts()</code>用作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的一部分。
这个函数会返回文章级别作为属性的object。如果你想知道保密的文章的字数,你应该使用isset() PHP函数,查看属性。并不是所有的文章级别都是object的一部分。
== Usage ==


== 用法 ==
== 用法 ==


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


%%% <?php wp_count_posts('type', 'readable'); ?> %%%
%%% <?php wp_count_posts('type', 'readable'); ?> %%%
== Examples ==
===Default Usage===


== 例子==
== 例子==
第33行: 第15行:
===默认用法===
===默认用法===


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()内容,调试输出结果。
默认用法,返回已经发表的文章的字数,这是object,你可以var_dump()内容,调试输出结果。
<pre><?php
<pre><?php
$count_posts = wp_count_posts();
$count_posts = wp_count_posts();
?></pre>
?></pre>
<pre><?php
$count_posts = wp_count_posts();
?></pre>
===Get the Publish Status Post Count===


===得到已经发表的文章的字数===
===得到已经发表的文章的字数===
To get the published status type, you would call the <code>wp_count_posts()</code> function and then access the ''''publish'''' property.


要得到已经发表的文章,你应该调用<code>wp_count_posts()</code>函数,然后访问''''发表''''属性。
要得到已经发表的文章,你应该调用<code>wp_count_posts()</code>函数,然后访问''''发表''''属性。
<pre><?php
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
?></pre>
<pre><?php
<pre><?php
$count_posts = wp_count_posts();
$count_posts = wp_count_posts();
第62行: 第28行:
$published_posts = $count_posts->publish;
$published_posts = $count_posts->publish;
?></pre>
?></pre>
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中运行,如果你想要维持向后兼容性,你必须使用以上的代码。
如果你只想得到一种级别的文章,如果你只是发展了PHP5,你可以使用速记。速记不能够在PHP4中运行,如果你想要维持向后兼容性,你必须使用以上的代码。
第70行: 第34行:
$published_posts = wp_count_posts()->publish;
$published_posts = wp_count_posts()->publish;
?></pre>
?></pre>
<pre><?php
$published_posts = wp_count_posts()->publish;
?></pre>
===Count Drafts===


===草稿的字数===
===草稿的字数===
Counting drafts is handled the same way as the publish status.


草稿字数的计算方法与已经发表的文章的计算方法是相同的。
草稿字数的计算方法与已经发表的文章的计算方法是相同的。
第88行: 第44行:
$draft_posts = $count_posts->draft;
$draft_posts = $count_posts->draft;
?></pre>
?></pre>
<pre><?php
$count_posts = wp_count_posts();
$draft_posts = $count_posts->draft;
?></pre>
===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.


计算网页的字数的方式与计算文章的字数方式是相同的,而且都是利用了第一个参数。计算出某个级别的文章有多少篇的方式与计算文章中有多少字数是相同的。
计算网页的字数的方式与计算文章的字数方式是相同的,而且都是利用了第一个参数。计算出某个级别的文章有多少篇的方式与计算文章中有多少字数是相同的。
<pre><?php
$count_pages = wp_count_posts('page');
?></pre>
<pre><?php
<pre><?php
$count_pages = wp_count_posts('page');
$count_pages = wp_count_posts('page');
?></pre>
?></pre>
===Other Uses===


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


The <code>wp_count_posts()</code> 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.
<code>wp_count_posts()</code>可以用来,找出任何类型的文章的文章不同级别的字数,包括附属,或者将来通过插件或者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''。默认为'''文章'''。
; 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'')使用'可读的',同时用户已经登录了,就可以阅读保密的文章。默认为''空字符串''
; perm: (''string'')使用'可读的',同时用户已经登录了,就可以阅读保密的文章。默认为''空字符串''

2008年7月25日 (五) 10:11的最新版本

描述[ ]

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

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

用法[ ]

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

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

例子[ ]

默认用法[ ]

默认用法,返回已经发表的文章的字数,这将是一个object,你可以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
(string) wp_posts中的row的type,计算出type在哪里等同于post_type。默认为文章
perm
(string)使用'可读的',同时用户已经登录了,就可以阅读保密的文章。默认为空字符串