get_children()

最后更新于:2021-11-26 07:38:21

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

Retrieve all children of the post parent ID.

参数

$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 GC_Post object, an associative array, or a numeric array, respectively.

Default value: OBJECT

响应

(GC_Post[]|int[]) Array of post objects or post IDs.

源文件

文件: gc-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,
	);

	$parsed_args = gc_parse_args( $args, $defaults );

	$children = get_posts( $parsed_args );

	if ( ! $children ) {
		return $kids;
	}

	if ( ! empty( $parsed_args['fields'] ) ) {
		return $children;
	}

	update_post_cache( $children );

	foreach ( $children as $key => $child ) {
		$kids[ $child->ID ] = $children[ $key ];
	}

	if ( OBJECT === $output ) {
		return $kids;
	} elseif ( ARRAY_A === $output ) {
		$weeuns = array();
		foreach ( (array) $kids as $kid ) {
			$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
		}
		return $weeuns;
	} elseif ( ARRAY_N === $output ) {
		$babes = array();
		foreach ( (array) $kids as $kid ) {
			$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
		}
		return $babes;
	} else {
		return $kids;
	}
}
<?php
/**
 * Echo first image (if available).
 *
 * @param int $post_id Post ID.
 */
function gcdocs_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 = gc_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? gc_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : gc_get_attachment_image_src( $attachment->ID, 'full' );
			echo '<img src="' . esc_url( gc_get_attachment_thumb_url( $attachment->ID ) ) . '" class="current" />';
		}
	}
}
$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.

$post_type ="post"; //page, or custom_post_type
$post_status = "any"; //publish, draft, etc
$num_of_posts = -1; // -1 for all, or amount # to return
$post_parent = 0; //0 for parents, or and id

$args = array('post_parent' => 0, 'post_type' => $post_type, 'numberposts' => $num_of_posts, 'post_status' => $post_status);

$parents = get_children($args);

foreach ($parents as $parent) {
 echo "<br>ParentID: ".$parent->ID;
}
&lt;?php
function print_first_kid_image_of_post($post_id) {
    $arr = array(
        'posts_per_page' => 1, # This param ensures only 1 post returned in the array
        'post_mime_type' => 'image',
        'post_parent'    => $post_id,
        'post_type'      => 'attachment',
    );
 
    $imgs = get_children($arr); # 响应 an array with an item
    $img = $imgs[0];

    $img_id = $img->ID;
	$img_size = 'thumbnail'
	echo gc_get_attachment_image($img_id, $img_size);
}
$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 gc_get_attachment_image( $attachment_id, 'full' );
	}
}

//  If you don't need to handle an empty result:

foreach ( (array) $videos as $attachment_id => $attachment ) {
	echo gc_get_attachment_link( $attachment_id );
}