get_children 根据父级文章信息文章的附件、版本、或子页面

发布于:
更新时间:2020-10-29

函数描述

get_children() 根据父级文章信息文章的附件、版本、或子页面,作用和 get_posts() 类似。

使用方法

<?php $children_array = get_children( $args, $output ); ?>

默认使用方法

$args = [
    'post_parent' => 0,
    'post_type'   => 'any',
    'numberposts' => -1,
    'post_status' => 'any',
];

参数

$args 数组中可用的选项有:

  • ‘numberposts’ (integer) (可选) 需要获取的文章数量。 默认: ‘-1’
  • ‘post_parent’ (integer) (可选) 传递文章或页面的ID以获取子内容,传递 0 获取没有父级的附件,传递 null 获取父级页面。 默认: ‘0’
  • ‘post_type’ (string) (可选) wp_posts数据表中的 post_type 列的任意值,如 attachment, page, 或 revision; 或者关键字 any 获取所有文章类型。 默认: ‘0’
  • ‘post_status’ (string) (可选) wp_posts 数据表中 post_status 列的任意值,如 publish, draft, 或 inherit; 或关键字 any 获取所有文章状态。 默认: ‘any’
  • ‘post_mime_type’ (string) (可选) 全不或部分mime-type, 如:image, video, video/mp4, 和文章的 spost_mime_type 字段相对应。 默认: None

注意:  查看 get_posts() 以 $args 参数的全部列表。 ‘output’ (constant) (可选) 函数返回的数组类型:OBJECT, ARRAY_A, ARRAY_N 中的一个。 默认: OBJECT

在版本2.6种,你必须传递一个非空的 post_type 参数 (attachment 或page)。

返回值

(array) 以文章ID作为数组key的关联文章数组($output参数设置的变量类型),获取失败返回空数组。

注意: 2.9以前的版本中,如果没有找到子文章,返回 false 。

使用示例

如果只是想显示附件,使用 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)) {
    // 没有图片
} else {
    foreach ($images as $attachment_id => $attachment) {
        echo wp_get_attachment_image($attachment_id, 'full');
    }
}

//  如果不需要处理空结果I:
foreach ((array)$videos as $attachment_id => $attachment) {
    echo wp_get_attachment_link($attachment_id);
}

显示关联到文章的第一张图片

下面的函数获取关联到文章的第一张图片。

function echo_first_image($postID)
{
    $args = [
        '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">';
        }
    }
}

显示关联到文章的第一张图片并且重新排列数组

在上面的示例中,主数组的键位图片ID,(确切的东西,被我们不知道我们怎么访问它?)。下面的这段代码提供了一个更方便的处理图片信息的方法:数组 $child_image,应该在文章循环中使用。

$args = [
    'numberposts'    => 1,
    'order'          => 'DESC',
    'post_mime_type' => 'image',
    'post_parent'    => $post->ID,
    'post_type'      => 'attachment',
];

$get_children_array = get_children($args, ARRAY_A);  //返回数组 ( [$image_ID]... 
$rekeyed_array      = array_values($get_children_array);
$child_image        = $rekeyed_array[ 0 ];

print_r($child_image);    //打印 $child_image 数组的内容

我们提供 WordPress主题和插件定制开发服务

本站长期承接 WordPress主题、插件、基于 WooCommerce 的商店商城开发业务。 我们有 10 年WordPress开发经验,如果你想 用WordPress开发网站, 请联系微信: iwillhappy1314,或邮箱: amos@wpcio.com 咨询。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

*