当前位置:首页>WordPress建站>WordPress开发>WordPress函数:get_children (获取子对象)

WordPress函数:get_children (获取子对象)

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. $defaults = array(
  2. 'post_parent' => 0,
  3. 'post_type' => 'any',
  4. 'numberposts' => -1,
  5. 'post_status' => 'any'
  6. );

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. $images =& get_children( 'post_type=attachment&post_mime_type=image' );
  2.  
  3. $videos =& get_children( 'post_type=attachment&post_mime_type=video/mp4' );
  4.  
  5. if ( empty($images) ) {
  6. // no attachments here【这里没有附件】
  7. } else {
  8. foreach ( $images as $attachment_id => $attachment ) {
  9. echo wp_get_attachment_image( $attachment_id, 'full' );
  10. }
  11. }
  12.  
  13. // If you don't need to handle an empty result:【如果你没必要处理一个空的结果:】
  14.  
  15. foreach ( (array) $videos as $attachment_id => $attachment ) {
  16. echo wp_get_attachment_link( $attachment_id );
  17. }

Show the first image associated with the post【展示与文章相关的第一张图片】

This function retrieves the first image associated with a post【下面的函数将检索与一篇文章相关的第一张图片】

  1. function echo_first_image ($postID)
  2. {
  3. $args = array(
  4. 'numberposts' => 1,
  5. 'order'=> 'ASC',
  6. 'post_mime_type' => 'image',
  7. 'post_parent' => $postID,
  8. 'post_status' => null,
  9. 'post_type' => 'attachment'
  10. );
  11. $attachments = get_children( $args );
  12. //print_r($attachments);
  13. if ($attachments) {
  14. foreach($attachments as $attachment) {
  15. $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' );
  16. echo '<img src="'.wp_get_attachment_thumb_url( $attachment->ID ).'" class="current">';
  17. }
  18. }
  19. }

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. $args = array(
  2. 'numberposts' => 1,
  3. 'order'=> 'DESC',
  4. 'post_mime_type' => 'image',
  5. 'post_parent' => $post->ID,
  6. 'post_type' => 'attachment'
  7. );
  8.  
  9. $get_children_array = get_children($args,ARRAY_A); //returns Array ( [$image_ID]...
  10. $rekeyed_array = array_values($get_children_array);
  11. $child_image = $rekeyed_array[0];
  12.  
  13.  
  14. print_r($child_image); //输出 $child_image 数组的内容
  15. 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()

参考资料:http://codex.wordpress.org/Function_Reference/get_children

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress函数:get_boundary_post(获取边界文章)

2013-3-21 6:54:00

WordPress开发

WordPress 自定义文章类型 介绍及实例解说(上)

2013-3-23 6:17:00

2 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
  1. cqhy

    倡老师!!我是个初学者,然后用这里的代码写了一个页面用做输出媒体库中的图片附件。
    $images = get_children(‘post_type=attachment’);
    //$images = get_posts(‘post_type=attachment’);
    if ( empty($images) ) {
    // no attachments here【这里没有附件】
    } else {
    foreach ( $images as $attachment_id => $attachment ) {
    /*echo $attachment_id.””;*/
    echo wp_get_attachment_image( $attachment_id, ‘thumbnail’ );
    }
    }

    就是这段代码,上面说get_children 和get_post类似么,但是只有get_children查询时能输出图片,我打印遍历后的attachment_id,只有get_children时能打印,是他们的返回结果不一样么?

  2. 倡萌讲一下小工具挂钩吧

个人中心
购物车
优惠劵
今日签到
私信列表
搜索