本文目录
[隐藏]
- 1Description【描述】
- 2Synopsis【简介】
- 3Return values【返回值】
- 4Default parameters (Version 2.7)【默认参数(版本 2.7)】
- 5Parameters【参数】
- 6Examples【例子】
- 6.1Show the first image associated with the post【展示与文章相关的第一张图片】
- 6.2Show the first image associated with the post and re-key the array【展示与文章 和 重新输入的数组 相关的第一张图片】
- 7Change Log【更新日志】
- 8Source File【源文件】
- 9Related【相关函数】
Description【描述】
get_children() retrieves attachments, revisions, or sub-Pages, possibly by post parent. It works similarly to get_posts().【get_children() 可以根据父级文章检索附件、修订版本 或者 子页面。它的作用和 get_posts() 相似。】
Synopsis【简介】
1 |
array $children =& get_children( mixed $args = "", constant $output = OBJECT); |
Return values【返回值】
Returns an associative array of posts (of variable type set by $outputparameter) with post IDs as array keys, or an empty array if no posts are found.【返回一组以文章ID作为数组值的关联文章(根据 $outputparameter 的变量类型设置);如果不存在文章,将返回一个空数组】
Prior to Version 2.9, the return value would be false when no children found.【在版本 2.9 之前,如果不存在子对象,返回的值将是错误的】
Default parameters (Version 2.7)【默认参数(版本 2.7)】
1 2 3 4 5 6 |
$defaults = array( 'post_parent' => 0, 'post_type' => 'any', 'numberposts' => -1, 'post_status' => 'any' ); |
Parameters【参数】
See get_posts() for a full list of parameters.【要查看所有的参数,请阅读 get_posts() 】
- $args
- (mixed) Passing a query-style string or array sets several parameters (below). Passing an integer post ID or a post object will retrieve children of that post; passing an empty value will retrieve children of the current post or Page. 【(mixed)通过 一个查询类型的字符串或数组 设置(下面)几个参数。通过一个 整数 的文章 ID 或 文章对象 将检索到那篇文章的子对象;过一个 空值 将检索到 当前文章或页面 的子对象。】
- $args[‘numberposts’]
- (integer) Number of child posts to retrieve. Optional; default: -1 (unlimited) 【(整数)要检索的子文章的数量。可选,默认:-1(不限制)】
- $args[‘post_parent’]
- (integer) Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Passnull to get any child regardless of parent. Optional; default: 0 (no parent) 【(整数)通过文章或页面的 ID 来检索它的子对象。通过 0 获取不包含父级的附件。通过 null 获取任何父级的子对象。可选,默认:0 (不包含父级)】
- $args[‘post_type’]
- (string) Any value from post_type column of the posts table, such as attachment, page, or revision; or the keyword any. Default: any 【(字符串)来自文章表的 post_type(文章类型) 那一栏的任何值,比如 附件、页面、修订版本 或 任何关键字。默认:any】
- $args[‘post_status’]
- (string) Any value from the post_status column of the wp_posts table, such as publish, draft, or inherit; or the keyword any. Default: any 【(字符串)来自 wp_posts 表的 post_status(文章状态) 那一栏的任何值,比如 已发布、草稿、继承 或任何关键字。默认:any】
- $args[‘post_mime_type’]
- (string) A full or partial mime-type, e.g. image, video, video/mp4, which is matched against a post’s post_mime_type field 【(字符串)一个完整的或部分的 mime-type,比如 图像、视频、视频/MP4,它和一篇文章的 post_mime_type 字段相匹配 】
- $output
- (constant) Variable type of the array items returned by the function: one of OBJECT, ARRAY_A, ARRAY_N. Optional; default: OBJECT【(常量)由该函数返回的数组项的变量类型:OBJECT, ARRAY_A, ARRAY_N 的其中一种。可选,默认:OBJECT】
Examples【例子】
If you just want to get or display attachments, it’s probably a little easier to use get_posts() instead.【如果你想要获取或显示附件,可能使用 get_posts() 会更加容易。】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$images =& get_children( 'post_type=attachment&post_mime_type=image' ); $videos =& get_children( 'post_type=attachment&post_mime_type=video/mp4' ); if ( empty($images) ) { // no attachments here【这里没有附件】 } else { foreach ( $images as $attachment_id => $attachment ) { echo wp_get_attachment_image( $attachment_id, 'full' ); } } // If you don't need to handle an empty result:【如果你没必要处理一个空的结果:】 foreach ( (array) $videos as $attachment_id => $attachment ) { echo wp_get_attachment_link( $attachment_id ); } |
Show the first image associated with the post【展示与文章相关的第一张图片】
This function retrieves the first image associated with a post【下面的函数将检索与一篇文章相关的第一张图片】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function echo_first_image ($postID) { $args = array( 'numberposts' => 1, 'order'=> 'ASC', 'post_mime_type' => 'image', 'post_parent' => $postID, 'post_status' => null, 'post_type' => 'attachment' ); $attachments = get_children( $args ); //print_r($attachments); if ($attachments) { foreach($attachments as $attachment) { $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' ); echo '<img src="'.wp_get_attachment_thumb_url( $attachment->ID ).'" class="current">'; } } } |
Show the first image associated with the post and re-key the array【展示与文章 和 重新输入的数组 相关的第一张图片】
In the example above, a primary array is keyed with the image ID (the exact thing which is being sought – since we don’t know it how are we supposed to access it?). The code below provides an easier handle for the image information: the array $child_image. Should be used in the loop.【在上面的例子中,一个最初的数组输入的是图片的 ID(正是要寻求的东西——因为我们不知道我们应该如何访问它)。下面的代码将提供一种更容易的处理图片信息的方式:数组 $child_image。它应该在循环中使用。】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$args = array( 'numberposts' => 1, 'order'=> 'DESC', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_type' => 'attachment' ); $get_children_array = get_children($args,ARRAY_A); //returns Array ( [$image_ID]... $rekeyed_array = array_values($get_children_array); $child_image = $rekeyed_array[0]; print_r($child_image); //输出 $child_image 数组的内容 echo $child_image['ID']; //输出 $child_image 的ID |
Change Log【更新日志】
Since: 2.0.0
Source File【源文件】
get_children() 在 wp-includes/post.php 文件中。
Related【相关函数】
get_children() calls get_posts(), which calls $WP_Query->get_posts().
wp_get_attachment_link(), get_page_children()