目录
描述
get_children() retrieves attachments, revisions, or sub-pages, possibly by post parent. It works similar to get_posts().
用法
<?php $children_array = get_children( $args, $output ); ?>
Default Usage
1 2 3 4 5 6 7 8 9 10 11 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ <?php $args = array( 'post_parent' => 0, 'post_type' => 'any', 'numberposts' => -1, 'post_status' => 'any' ); ?> |
参数
As of Version 2.6, you must pass a non-empty post_type parameter (either attachment or page).
The following options are available in the $args array:
'numberposts'
(integer) (可选) Number of child posts to retrieve.
默认值: '-1'
'post_parent'
(integer) (可选) Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Pass null to get any child regardless of parent.
默认值: '0'
'post_type'
(string) (可选) Any value from post_type column of the posts table, such as attachment, page, or revision; or the keyword any.
默认值: '0'
'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.
默认值: 'any'
'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.
默认值: None
Note: See get_posts() for a full list of $args parameters.
'output'
(constant) (可选) Variable type of the array items returned by the function: one of OBJECT, ARRAY_A, ARRAY_N.
默认值: OBJECT
返回值
(array)
Associative array of posts (of variable type set by $output parameter) with post IDs as array keys, or an empty array if no posts are found.
Note: Prior to Version 2.9, the return value would be false when no children found.
get_children() 返回值
示例
If you just want to get or display attachments, it's probably a little easier to use get_posts()
instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ $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 |
<?php 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 ); 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.
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); //Show the contents of the $child_image array. echo $child_image['ID']; //Show the $child_image ID. |
历史
添加于 版本: 2.0.0
源文件
get_children() 函数的代码位于 wp-includes/post.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ /** * Retrieve all children of the post parent ID. * * Normally, without any enhancements, the children would apply to pages. In the * context of the inner workings of WordPress, pages, posts, and attachments * share the same table, so therefore the functionality could apply to any one * of them. It is then noted that while this function does not work on posts, it * does not mean that it won't work on posts. It is recommended that you know * what context you wish to retrieve the children of. * * Attachments may also be made the child of a post, so if that is an accurate * statement (which needs to be verified), it would then be possible to get * all of the attachments for a post. Attachments have since changed since * version 2.5, so this is most likely unaccurate, but serves generally as an * example of what is possible. * * The arguments listed as defaults are for this function and also of the * {@link get_posts()} function. The arguments are combined with the * get_children defaults and are then passed to the {@link get_posts()} * function, which accepts additional arguments. You can replace the defaults in * this function, listed below and the additional arguments listed in the * {@link get_posts()} function. * * The 'post_parent' is the most important argument and important attention * needs to be paid to the $args parameter. If you pass either an object or an * integer (number), then just the 'post_parent' is grabbed and everything else * is lost. If you don't specify any arguments, then it is assumed that you are * in The Loop and the post parent will be grabbed for from the current post. * * The 'post_parent' argument is the ID to get the children. The 'numberposts' * is the amount of posts to retrieve that has a default of '-1', which is * used to get all of the posts. Giving a number higher than 0 will only * retrieve that amount of posts. * * The 'post_type' and 'post_status' arguments can be used to choose what * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress * post types are 'post', 'pages', and 'attachments'. The 'post_status' * argument will accept any post status within the write administration panels. * * @since 2.0.0 * * @see get_posts() * @todo Check validity of description. * * @global WP_Post $post * * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. * @param string $output Optional. Constant for return type. Accepts OBJECT, ARRAY_A, ARRAY_N. * Default OBJECt. * @return array Array of children, where the type of each element is determined by $output parameter. * Empty array on failure. */ function get_children( $args = '', $output = OBJECT ) { $kids = array(); if ( empty( $args ) ) { if ( isset( $GLOBALS['post'] ) ) { $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); } else { return $kids; } } elseif ( is_object( $args ) ) { $args = array('post_parent' => (int) $args->post_parent ); } elseif ( is_numeric( $args ) ) { $args = array('post_parent' => (int) $args); } $defaults = array( 'numberposts' => -1, 'post_type' => 'any', 'post_status' => 'any', 'post_parent' => 0, ); $r = wp_parse_args( $args, $defaults ); $children = get_posts( $r ); if ( ! $children ) return $kids; if ( ! empty( $r['fields'] ) ) return $children; update_post_cache($children); foreach ( $children as $key => $child ) $kids[$child->ID] = $children[$key]; if ( $output == OBJECT ) { return $kids; } elseif ( $output == ARRAY_A ) { $weeuns = array(); foreach ( (array) $kids as $kid ) { $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); } return $weeuns; } elseif ( $output == ARRAY_N ) { $babes = array(); foreach ( (array) $kids as $kid ) { $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); } return $babes; } else { return $kids; } } |
get_children() 源文件
- 原文:http://codex.wordpress.org/Function_Reference/get_children
- 翻译:黄聪@WordPress之魂