我在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])。
我的數組知識是有限的,所以我不知道如何做到這一點..任何幫助表示讚賞。
@AdvaitAmin,請不要在將來像'array-functions'一樣創建[meta tags](http://blog.stackoverflow.com/2010/08/the-death-of-meta-tags/)。 – Charles 2013-03-26 17:24:25
@Charles瞭解不多,你能用你的話簡單解釋嗎? – 2013-03-28 07:49:13