2013-03-25 110 views
1

我在Wordpress中有一些代碼可以獲取附加到特定帖子的圖像。
它把該職位的所有信息在一個數組然後使用從它拉圖像內置的WordPress的功能wp_get_attachment_image更改數組的順序

然後我使用foreach環在圖像滑塊顯示圖像。 我遇到的問題是它將'image1'放在數組的第一位,'image2'放在數組的第0位。
所以它首先顯示圖像2。

這是我的代碼。

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

    $attachments = get_posts($args);   
    $images = array($attachments); 
    echo '<div id="postSlider"><div class="slides_container">'; 
    if ($attachments) { 
     foreach ($attachments as $attachment) { 
     echo '<div>' . wp_get_attachment_image($attachment->ID, 'large') . '</div>'; 
     } 

     echo '</div>'; 
     if(sizeof($attachments) > 1) { 
      echo '<div class="sliderControls"> 
      <a href="#" class="sliderBtnPrev">Previous</a> 
      <a href="#" class="sliderBtnNext">Next</a> 
      <span class="sliderPagination">1 of 3</span> 
      </div>'; 
     } 
    } 
    echo '</div>'; 

從我讀的foreach循環將保持數組的順序。所以,我想我需要改變數組的順序,以便循環首先看到'image1'(數組位置[1])。

我的數組知識是有限的,所以我不知道如何做到這一點..任何幫助表示讚賞。

+0

@AdvaitAmin,請不要在將來像'array-functions'一樣創建[meta tags](http://blog.stackoverflow.com/2010/08/the-death-of-meta-tags/)。 – Charles 2013-03-26 17:24:25

+0

@Charles瞭解不多,你能用你的話簡單解釋嗎? – 2013-03-28 07:49:13

回答

1

如果你想以編程方式更改順序,有一個看看各種array sorting functions in PHP,特別是

  • uasort() - 用一個用戶自定義的比較函數和maint AIN索引關聯
  • uksort() - 按使用一個用戶定義的比較函數
  • usort()鍵的陣列 - 按使用一個用戶定義的比較函數

值,但爲WordPress結帳此http://codex.wordpress.org/Template_Tags/get_posts

陣列
+1

感謝鏈接到'get_posts' 這給了我需要的信息 – 2013-03-25 19:55:49

0

如果你只想在foreach之前,以顯示排序順序使用sort圖像MAES

sort($attachments); 
    foreach ($attachments as $attachment) { 
    echo '<div>' . wp_get_attachment_image($attachment->ID, 'large') . '</div>'; 
    } 

編號:http://php.net/manual/en/function.sort.php

+0

@PrashanthBendra在數組標籤問題你的答案總是存在.... – 2013-03-25 12:28:08

+0

@AdvaitAmin:是的...我認爲我擅長:P – 2013-03-25 12:29:12

+0

@PrashanthBendra是的兄弟你好陣列及其功能...可能是你的答案始終在5分鐘內......頭朝下...... – 2013-03-25 12:33:58