无法让 Wordpress 自定义 post 类型的附件在索引数组中运行

Can't get Wordpress custom post type attachments to function in indexed array

搜索了好几个小时都没有成功。我的问题很简单,但答案似乎难以捉摸。我想在自定义 post 类型的每个 post 中分别显示前三个附件图像。以下代码让我走到了那里,但我不确定如何继续?

<?php
$attachments = get_children( array(
    'post_parent'    => get_the_ID(),
    'post_type'      => 'attachment',
    'numberposts'    => -1,
    'post_status'    => 'inherit',
    'post_mime_type' => 'image',
    'order'          => 'ASC',
    'orderby'        => 'menu_order ASC'
    ) );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'medium');
} ?>

好的,在此处添加了附件转储:

Array
(
[106] => WP_Post Object
(
    [ID] => 106
    [post_author] => 2
    [post_date] => 2013-03-13 13:38:11
    [post_date_gmt] => 2013-03-13 12:38:11
    [post_content] => 
    [post_title] => autumn_leaves_new2
    [post_excerpt] => 
    [post_status] => inherit
    [comment_status] => open
    [ping_status] => open
    [post_password] => 
    [post_name] => autumn_leaves_new2
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2015-08-15 06:01:05
    [post_modified_gmt] => 2015-08-15 06:01:05
    [post_content_filtered] => 
    [post_parent] => 278
    [guid] => http://localhost/pbvcid/wp
content/uploads/2013/03/autumn_leaves_new2.jpg
    [menu_order] => 0
    [post_type] => attachment
    [post_mime_type] => image/jpeg
    [comment_count] => 0
    [filter] => raw
    )

我想我的解释需要更清楚一点。由于我的 php 知识有限,我提供的代码只是一个开始,需要扩展。我相信我正在寻找的是一种将自定义 post 类型附件放入索引数组中的方法,这样我就可以从该数组中选择一些 individual 附件并在它们自己的中显示它们在dividual div。到目前为止,代码运行效率不高,也没有显示 post 中的所有附件(顺便说一句,感谢迄今为止的所有帮助)。

尝试使用以下代码获取附件。

<?php
$attachments = get_children( array(
    'post_parent'    => get_the_ID(),
    'post_type'      => 'attachment',
    'numberposts'    => 3,
    'post_status'    => 'inherit',
    'post_mime_type' => 'image',
    'order'          => 'ASC',
    'orderby'        => 'menu_order ASC'
    ) );
foreach ( $attachments as $attachment_id => $attachment ) {
    echo wp_get_attachment_image( $attachment_id, 'medium');
} ?>

试试这个

$attachments = get_children( array(
    'post_parent'    => get_the_ID(),
    'post_type'      => 'attachment',
    'numberposts'    => -1,
    'post_status'    => 'inherit',
    'post_mime_type' => 'image',
    'order'          => 'ASC',
    'orderby'        => 'menu_order ASC'
 ));
foreach ( $attachments as $image ) {
    echo wp_get_attachment_image( $image->ID, 'medium' );
}

如果这不起作用转储你的附件变量,post结果返回这里,

你可以像这样转储它

echo '<pre>', print_r($attachments, 1), '</pre>';

找到正确显示数组的解决方案:

$attachments = get_children( array(
    'post_parent'    => get_the_ID(),
    'post_type'      => 'attachment',
    'numberposts'    => -1,
    'post_status'    => 'inherit',
    'post_mime_type' => 'image',
    'order'          => 'ASC',
    'orderby'        => 'menu_order ASC'
    ) );

    $imgArray = array();
    $counter = 0;
    foreach ( $attachments as $attachment_id => $attachment ) {

    $imgArray[$counter] = $attachment_id;
    $counter++;
    if($counter > 2) {
    break;
    }
    } 
    ?>
<div class="attachment1"><?php echo wp_get_attachment_image( $imgArray[0], 'medium'); ?></div>
<div class="attachment2"><?php echo wp_get_attachment_image( $imgArray[1], 'medium'); ?></div>
<div class="attachment3"><?php echo wp_get_attachment_image( $imgArray[2], 'medium'); ?></div>