WordPress函数get_children()获取子对象
在 WordPress CMS内容管理系统中,get_children()是一个内置函数,get_children () WordPress PHP 函数检索帖子父 ID 的所有子项。它可以应用于页面、帖子和附件
推荐:什么是Trackback?WordPress怎么禁用Trackback
get_children()函数基本语法
描述
检索帖子父 ID 的所有子项
用法
<?php get_children($args, $output);; ?>
参数
$args
( mixed ) ( 可选) 用于替换默认值的用户定义参数。默认:''
$output
(string)(可选)必需的返回类型。OBJECT、ARRAY_A 或 ARRAY_N 之一,分别对应于 WP_Post 对象、关联数组或数值数组。默认值:OBJECT
get_children()函数
通常,在没有任何增强的情况下,会应用于页面。然后需要注意的是,虽然这个功能对帖子不起作用,但这并不意味着它不会对帖子起作用。建议您知道要检索其子项的上下文。(源文件可参考这里)
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 = wp_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;
}
}
推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons
如何使用get_children()
显示与帖子关联的第一张图片
function display_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 ) {
echo '<img src="' . esc_url( wp_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 );
$rekeyed_array = array_values( $get_children_array );
$child_image = $rekeyed_array[0];
获取所有父帖子 ID
$post_type = "post";
$post_status = "any";
$num_of_posts = -1;
$post_parent = 0;
$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;
}
仅获取一组子 ID
$child_ids = get_children( [
'post_parent' => $post_id,
'fields' => 'ids',
] );
推荐:[最新版]Asset CleanUp Pro插件免费下载WordPress性能优化插件
Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折