Foreach从最后到第一个显示

时间:2014-06-26 作者:Mateusz Winnicki

我创建了foreach循环来显示post图像,但它从最后一个到第一个显示这些图像,如何使其从第一个到最后一个显示;)?

     <?php $args = array(
     \'post_type\' => \'attachment\',
    \'numberposts\' => -1,
      \'post_status\' => null,
       \'post_parent\' => $post->ID
     );

     $attachments = get_posts( $args );

 if ( $attachments ) {
   foreach ( $attachments as $attachment ) : ?>
   <p>                    
      <?php echo wp_get_attachment_image( $attachment->ID, \'full\' ); ?>
      </p>

   <?php endforeach;    

 }
   ?>

2 个回复
最合适的回答,由SO网友:Joey Yax 整理而成

为什么不按需要的顺序拉动它们,而不是反转阵列?

$args = array(
  \'post_type\' => \'attachment\',
  \'posts_per_page\' => -1, // correct key is posts_per_page, not numberposts
  \'orderby\' => \'date\'
  \'order\' => \'ASC\',
  \'post_parent\' => $post->ID
);

$attachments = get_posts( $args );

SO网友:vancoder

重新反转阵列!

     if ( $attachments ) {

$attachments = array_reverse( $attachments );

       foreach ( $attachments as $attachment ) : ?>
       <p>                    
          <?php echo wp_get_attachment_image( $attachment->ID, \'full\' ); ?>
          </p>

       <?php endforeach;    

     }

结束