您不必合并数组来在循环中使用它们。您只需要一个可以用作哈希查找的类似密钥。那就是wp_list_pluck
很方便。
// Image Data
$instagrams = array (
array (
\'post_id\' => 1,
\'image\' => \'http://placekitten.com/200/300\',
), array (
\'post_id\' => 2,
\'image\' => \'http://placekitten.com/200/300\',
), array (
\'post_id\' => 3,
\'image\' => \'http://placekitten.com/200/300\',
),
);
// Transform the array where post_id is the key
$hash = wp_list_pluck( $instagrams, \'image\', \'post_id\' );
// WP_Query arguments
$args = array (
\'post_type\' => array ( \'post\' ),
\'post_status\' => array ( \'publish\' ),
\'posts_per_page\' => \'3\',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// the ID from the post
$post_id = get_the_ID();
// becomes the key to the image array
if ( isset( $hash[ $post_id ] ) ) {
$image = $hash[ $post_id ];
echo $image;
}
}
}
wp_reset_postdata();
其他技术包括使用数组进行循环
get_posts
, 使用自定义排序
usort
或重复使用循环
rewind_posts
-- 这取决于你想变得多聪明。