^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_children()用法示例

get_children( mixed $args = '',  string $output = OBJECT )

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.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

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 inaccurate, but serves generally as an example of what is possible.

The arguments listed as defaults are for this function and also of the get_posts() function. The arguments are combined with the get_children defaults and are then passed to the get_posts() function, which accepts additional arguments. You can replace the defaults in this function, listed below and the additional arguments listed in the 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.


参数

$args

(mixed)
(Optional)
User defined arguments for replacing the defaults.

Default value: ”

$output

(string)
(Optional)
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.

Default value: OBJECT


返回值

(array) Array of children, where the type of each element is determined by $output parameter. Empty array on failure.


源代码

File: wp-includes/post.php

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;
	}
}

更新日志

Version 描述
2.0.0 Introduced.

相关函数

Uses

  • wp-includes/functions.php:
    wp_parse_args()
  • wp-includes/post.php:
    update_post_cache()
  • wp-includes/post.php:
    get_posts()

Used By

  • wp-admin/includes/media.php:
    get_media_items()
  • wp-includes/media.php:
    get_attached_media()
  • wp-includes/media.php:
    adjacent_image_link()
  • wp-includes/media.php:
    gallery_shortcode()
  • wp-includes/media.php:
    wp_playlist_shortcode()
  • wp-includes/revision.php:
    wp_get_post_revisions()
  • Show 1 more used by
    Hide more used by


  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    If you just want to get or display attachments, it’s probably a little easier to use get_posts() instead.

    
    $images =& get_children( array(
    	'post_type'      => 'attachment',
    	'post_mime_type' => 'image'
    );
    
    $videos =& get_children( array(
    	'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 );
    }
    
  2. Show the first image associated with the post

    This function retrieves the first image associated with a post

    
    <?php
    /**
     * Echo first image (if available).
     *
     * @param int $post_id Post ID.
     */
    function wpdocs_echo_first_image( $post_id ) {
    	$args = array(
    		'posts_per_page' => 1,
    		'order'          => 'ASC',
    		'post_mime_type' => 'image',
    		'post_parent'    => $post_id,
    		'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="' . esc_url( 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.

    
    $args = array(
    	'posts_per_page' => 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.
    

    Each item returned by get_children() has the following values (names shown are mapped from ARRAY_A; same names are used with OBJECT):

    array(24) {
    ["ID"] => (int)
    ["post_author"] => (string)
    ["post_date"] => (string)
    ["post_date_gmt"] => (string)
    ["post_content"] => (string)
    ["post_title"] => (string)
    ["post_excerpt"] => (string)
    ["post_status"] => (string)
    ["comment_status"] => (string)
    ["ping_status"] => (string)
    ["post_password"] => (string)
    ["post_name"] => (string)
    ["to_ping"] => (string)
    ["pinged"] => (string)
    ["post_modified"] => (string)
    ["post_modified_gmt"] => (string)
    ["post_content_filtered"] => (string)
    ["post_parent"] => (int)
    ["guid"] => (string)
    ["menu_order"] => (int)
    ["post_type"] => (string)
    ["post_mime_type"] => (string)
    ["comment_count"] => (string)
    ["filter"] => (string)
    }

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈