描述:
获取子对象
用法:
<?php
$args = array(
'post_parent' => 0,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
?>
参数:
从2.6版起,必须传递非空post_类型参数(附件或页面)。
$args数组中提供以下选项:
'numberposts'
(integer) (可选) 要检索的子帖子数。
默认值: '-1'
'post_parent'
(integer) (可选) 传递帖子或页面的ID以获取其子级。传递0以获取没有父级的附件。传递null以获取任何子级,而不考虑父级。
默认值: '0'
'post_type'
(string) (可选) posts表的post_type列中的任何值,例如attachment、page或revision;或关键字Any。
默认值: '0'
'post_status'
(string) (可选) wp_posts表post_status列中的任何值,如publish、draft或inherit;或关键字Any。
默认值: 'any'
'post_mime_type'
(string) (可选) 与post的post-mime mime type字段匹配的完整或部分mime类型,如image、video/mp4。
默认值: None
注意: 有关$args参数的完整列表,请参见get_posts()。
'output'
(constant) (可选) 函数返回的数组项的变量类型:OBJECT、ARRAY_A、ARRAY_N。
默认值: OBJECT
示例:
如果您只想获取或显示附件,那么使用get_posts()可能会容易一些。
$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 );
}
显示与帖子关联的第一个图像
此函数用于检索与post关联的第一个图像
<?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">';
}
}
}
显示与post关联的第一个图像并为数组重新设置键
在上面的例子中,一个主数组是用图像ID(正被寻找的东西-因为我们不知道我们应该如何访问它?)。下面的代码为图像信息提供了一个更简单的句柄:数组$child_image。应该在循环中使用。
$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.
源文件:
/**
* 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;
}
}