我有一些特殊的post
,来自category
“图库”,我希望恢复所有attached media images
。我遍历posts
,然后我获取所有媒体,并将其保存在associative array
中,但结果如下:
代码(仅限相关)
<?php $args=array(
'category_name' => $gallery_category_slug,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby'=> 'ID',
'order' => 'asc',
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo get_the_ID(); ?> *ID of each post (OK)
<?php the_title(); ?> *Title of each post (Ok)
<?php
//Get Attached images from category (post)
$args_att = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => get_the_ID() //Same ID always
);
$attachments = get_posts( $args_att );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$aux = wp_get_attachment_image_src($attachment->ID , 'full' );
if($aux){
$post_images[get_the_ID()][]= $aux[0];
}
}
}
?>
<?php endwhile;?>
结果:
Array ( [186] => Array ( [0] => http://testdomain/wp-content/uploads/2016/06/WINE.jpg [1] => http://testdomain/wp-content/uploads/2016/06/MASTERBEDROOM.jpg [2] => http://testdomain/wp-content/uploads/2016/06/POOL.jpg [3] => http://testdomain/wp-content/uploads/2016/06/TENNIS.png [4] => http://testdomain/wp-content/uploads/2016/06/ENTERTAINMENT.jpg [5] => http://testdomain/wp-content/uploads/2016/06/EXCERCISE.jpg [6] => http://testdomain/wp-content/uploads/2016/06/BEDROOMS.png [7] => http://testdomain/wp-content/uploads/2016/06/DSC_5854.jpg [8] => http://testdomain/wp-content/uploads/2016/06/DSC_5800.jpg ) )
期望的结果:
Array ( [186] => Array ( [0] => http://testdomain/wp-content/uploads/2016/06/WINE.jpg [1] => http://testdomain/wp-content/uploads/2016/06/MASTERBEDROOM.jpg [2] => http://testdomain/wp-content/uploads/2016/06/POOL.jpg ), [187] => Array ( [0] => http://testdomain/wp-content/uploads/2016/06/WINE.jpg [1] => http://testdomain/wp-content/uploads/2016/06/MASTERBEDROOM.jpg ), [188] => Array ( [0] => http://testdomain/wp-content/uploads/2016/06/WINE.jpg ))
我认为我弄乱了数组。
答案 0 :(得分:0)
以下是我的理论:我认为问题出在get_the_ID()
中的$post_images[get_the_ID()][]= $aux[0];
,因为get_posts
更改了$post
使用的全局get_the_ID()
对象。所以,我将帖子的ID放入变量中,然后在函数的其余部分使用它。
希望这有帮助。
<?php
$args=array(
'category_name' => $gallery_category_slug,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby'=> 'ID',
'order' => 'asc',
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$original_post_ID = get_the_ID();
the_title();
//Get Attached images from category (post)
$args_att = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $original_post_ID //our new variable
);
$attachments = get_posts( $args_att );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$aux = wp_get_attachment_image_src($attachment->ID , 'full' );
if($aux){
$post_images[$original_post_ID][]= $aux[0];
}
}
}
endwhile;
?>