2013-01-15 30 views
0

我有幾個使用Wordpress作爲CMS的網站。都使用下面的代碼使用自定義渲染畫廊。如何調用WordPress 3.5圖庫圖片,以便重新訂購它們?

<?php 
$the_cat = get_the_category(); 
$category_name = $the_cat[0]->cat_name; 
$category_description = $the_cat[0]->category_description; 
$category_link = get_category_link($the_cat[0]->cat_ID); 
$url = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 

echo '<div class="titleblock-2"><p>'; 
echo customTitle(50); 
echo '</p></div>'; 

$attachments = get_posts(array 
    (
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => 'published', 
    'post_parent' => $post->ID, 
    'orderby' => 'menu_order', 
    'order' => '' 
    ) 
); 

if ($attachments) { 
    echo '<div id="singlegallery">'; 
    foreach ($attachments as $attachment) { 
     $attachment_page = get_attachment_link($attachment->ID); 
     echo '<div class="big-thumb"><div class="gallery-icon"><a href="'; 
     echo wp_get_attachment_url($attachment->ID); 
     echo '">'; 

     $myimage = preg_replace('/(width|height)=\"\d*\"\s/', "", wp_get_attachment_image($attachment->ID, medium)); echo $myimage; 

     echo '</a></div></div>'; 
     } 
    echo '</div>'; 
    } 

?> 

我的問題是,WordPress的3.5似乎並沒有被處理以前上傳的圖片的職位,因爲它用,雖然該代碼會產生一個畫廊,圖像不能在WP管理界面內重新排序。

我已經看到了建議,附件,因爲他們沒有不再工作,並有另一種方式使用這樣的代碼來調用圖片:

$post_content = $post->post_content; 
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids); 
$array_id = explode(",", $ids[1]); 

有人可以幫助解釋這到底是怎麼應用?從頭開始重新創建每個畫廊的'解決方案'在這裏不是一種選擇。

回答

0

正如我所提到here,你只需要擴大你的if ($attachments)條款:

<?php 
$the_cat = get_the_category(); 
$category_name = $the_cat[0]->cat_name; 
$category_description = $the_cat[0]->category_description; 
$category_link = get_category_link($the_cat[0]->cat_ID); 
$url = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 

echo '<div class="titleblock-2"><p>'; 
echo customTitle(50); 
echo '</p></div>'; 

$attachments = get_posts(array 
    (
    'post_type' => 'attachment', 
    'numberposts' => -1, 
    'post_status' => 'published', 
    'post_parent' => $post->ID, 
    'orderby' => 'menu_order', 
    'order' => '' 
    ) 
); 

if ($attachments) { 
    echo '<div id="singlegallery">'; 
    foreach ($attachments as $attachment) { 
     $attachment_page = get_attachment_link($attachment->ID); 
     echo '<div class="big-thumb"><div class="gallery-icon"><a href="'; 
     echo wp_get_attachment_url($attachment->ID); 
     echo '">'; 

     $myimage = preg_replace('/(width|height)=\"\d*\"\s/', "", wp_get_attachment_image($attachment->ID, medium)); echo $myimage; 

     echo '</a></div></div>'; 
     } 
    echo '</div>'; 
} else { 
    $post_content = $post->post_content; 
    preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids); 
    $attachment_ids = explode(",", $ids[1]); 
    foreach ($attachment_ids as $attachment_id) { 

     // here you can use your old code for printing images, but use "$attachment_id" instead of "$attachment->ID" 
    } 
} 

?> 
+0

這是偉大的斯萊文。謝謝。 –